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

Support complex properties

Open osmozis opened this issue 1 year ago • 0 comments

``Data is not saved to table columns mapped as complex properties. If these columns are set to NOT NULL in database, then exception is thrown

For example:

create table positions(
  [id] int identity(1,1) not null primary key,
  [position_x] real, --not null
  [position_y] real, --not null
  [position_z] real --not null
);
record Position(float X, float Y, float Z);

class Item
{
   public int Id { get; set; }
   public Position Position {get; set;}
}

modelBuilder.Entity<Item>(f =>
{
    f.ToTable("positions");
    f.HasKey(x => x.Id);
    f.Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
    f.ComplexProperty(x => x.Position).IsRequired();
});

test method:

var items = new List<Item>();

for (var i = 1; i <= 1_000; ++i)
{
    var item = new Item
    {
        Position = new Position(0, 0, 0)
    };

    items.Add(item);
}

// this code correctly sets position_x, position_y and position_z values as 0, 0 and 0
//context.AddRange(items);
//context.SaveChanges();

// this code sets position_x, position_y and position_z values as NULL, NULL and NULL
context.BulkInsert(items);
context.BulkSaveChanges();

osmozis avatar May 13 '24 08:05 osmozis