You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have an optional Owned Entity Type configured for an entity. Example: User has an optional owned entity type Address. We use BulkMergeAsync to insert/update new User entities into the database.
When we set the optional Address property to null in the C# objects, the values aren't updated and won't get set to null in the database when using BulkMergeAsync.
Fiddle or Project (Optional)
The following sample program shows the behavior:
We insert a new user where Address properties and the optional Info property are set to some values using BulkMergeAsync.
We update the existing user: We set Address and Info to null and call BulkMergeAsync again.
Expected behavior: All Address properties and the Info property are set to null.
Actual behavior: The Info property is set to null (as expected) but all Address properties are still set to the original values.
using Microsoft.EntityFrameworkCore;using(varcontext=new UserStore()){// Cleanup users for testingawait context.Users.DeleteFromQueryAsync();}using(varcontext=new UserStore()){varusers=newList<User>(){new User(new Guid("00000000-0000-0000-0000-000000000001"),"Name 1"){Address=new Address("City 1","Street 1"),Info="Info 1"},};await context.BulkMergeAsync(users);}
Console.WriteLine();
Console.WriteLine("AFTER FIRST BULK MERGE");
Console.WriteLine();using(varcontext=new UserStore()){
context.Users.ToList().ForEach(Console.WriteLine);varusers=newList<User>(){// Setting Info and Address to nullnew User(new Guid("00000000-0000-0000-0000-000000000001"),"Name 1"){Address=null,Info=null},};await context.BulkMergeAsync(users,opts => opts.IncludeGraph =true);}
Console.WriteLine();
Console.WriteLine("AFTER FIRST BULK MERGE");
Console.WriteLine();using(varcontext=new UserStore()){
context.Users.ToList().ForEach(Console.WriteLine);// <-- 'Info' property is correctly set to null but 'Address' (Owned Entity) properties are still set.}publicclassUser{publicUser(Guidid,stringname){Id=id;Name=name;}publicGuidId{get;set;}publicstringName{get;set;}publicstring?Info{get;set;}publicAddress?Address{get;set;}publicoverridestringToString(){return$"Name: {Name}\nInfo: {Info}\nAddress: {Address?.Street}{Address?.City}";}}publicclassAddress{publicAddress(stringstreet,stringcity){Street=street;City=city;}publicstringStreet{get;set;}publicstringCity{get;set;}}publicclassUserStore:DbContext{protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder){
optionsBuilder
.UseSqlServer(ConnectionString.Value);}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().OwnsOne(u => u.Address);}publicDbSet<User> Users =>Set<User>();}
Output
AFTER FIRST BULK MERGE
Name: Name 1
Info: Info 1
Address: City 1 Street 1
AFTER FIRST BULK MERGE
Name: Name 1
Info:
Address: City 1 Street 1 // <-- here we would expect null values
Further technical details
EF version: Microsoft.EntityFrameworkCore.SqlServer 6.0.0
EF Extensions version: 6.13.12
Database Provider: SQL Server
The text was updated successfully, but these errors were encountered:
Description
We have an optional Owned Entity Type configured for an entity. Example:
User
has an optional owned entity typeAddress
. We useBulkMergeAsync
to insert/update newUser
entities into the database.When we set the optional
Address
property tonull
in the C# objects, the values aren't updated and won't get set tonull
in the database when usingBulkMergeAsync
.Fiddle or Project (Optional)
The following sample program shows the behavior:
Address
properties and the optionalInfo
property are set to some values usingBulkMergeAsync
.Address
andInfo
tonull
and callBulkMergeAsync
again.Address
properties and theInfo
property are set tonull
.Info
property is set to null (as expected) but allAddress
properties are still set to the original values.Output
Further technical details
Microsoft.EntityFrameworkCore.SqlServer
6.0.0The text was updated successfully, but these errors were encountered: