N.EntityFrameworkCore.Extensions icon indicating copy to clipboard operation
N.EntityFrameworkCore.Extensions copied to clipboard

Existing Guid based ID columns do not work...

Open davidcorbin-atmosera opened this issue 1 year ago • 2 comments

Describe the bug Create a class with a Guid ID that is pre-loaded... it can not be bulk inserted...

To Reproduce Steps to reproduce the behavior:

  1. Clone BulkInsert.With_Complex_Key
  2. Create class ProductWithExistingGuidKey with an ID field (initialized in Constructor to new Guid)

Expected behavior Items are bulk inserted.

Screenshots image

Desktop (please complete the following information):

  • OS: Windo2w 11.
  • N/A
  • Fresh clone of repository a few hours ago.

Additional context Add any other context about the problem here.

davidcorbin-atmosera avatar Jun 19 '24 11:06 davidcorbin-atmosera

@davidcorbin-atmosera,

I am not sure if this is a bug or just a lack of a good error message. You specified an ID column and by default EF will identify it as a primary key column with ValuedGenerated=OnAdd. The extension library is expecting the database to generate the id column. In order to fix this error you would have to specify the "[DatabaseGenerated(DatabaseGeneratedOption.None)]" attribute on the Id column as shown below. I will do some more research on this issue in order to validate my initial findings and I will report what I discover.

public class ProductWithExistingKey { [DatabaseGenerated(DatabaseGeneratedOption.None)] public Guid Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public ProductWithExistingKey() { Id = Guid.NewGuid(); } }

NorthernLight1 avatar Jun 19 '24 20:06 NorthernLight1

First, thanks for getting back to me so fast.... My expectation (which could be off-base)... is that code that works with DbSet<Entity>.Add(entity) followed by a Context.SaveChanges(). would work without changes when moving to Bulk operations on the same set of entities (into the same Db, etc...)

Yup.... This works,,, but replace back the one line and it fails.... interesting to say the least....

image

davidcorbin-atmosera avatar Jun 19 '24 20:06 davidcorbin-atmosera