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
It seems that when using DbContextBulkTransactionSaveChanges.SaveChangesAsync method the code groups changed entities by EntityType using the Change tracker:
The problem however is that this will also group OwnedEntities (cases where you have an Entity that owns another entity (ValueObject))) per type and causes an object is not an instance error later on when trying to Find the entity type using the context.Model
Code snipper to create an entity with an owned entity:
Entity type configuration:
The text was updated successfully, but these errors were encountered:
Here is the code I used for the EF Unit test project to test this issue:
Model:
public record InfoOwnedEntity(string Mobile, string AlternateMobile);
public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
[MaxLength(50)]
public string Description { get; set; }
public int Quantity { get; set; }
public decimal? Price { get; set; }
public DateTime? TimeUpdated { get; set; }
public InfoOwnedEntity OwnedEntity { get; set; }
}
Entity configuration: modelBuilder.Entity<Item>().OwnsOne(p => p.OwnedEntity, s => s.WithOwner());
Unit test:
[Fact]
public async Task EF_OwnedEntityTest_SaveChangesAsync()
{
ContextUtil.DbServer = DbServer.SQLServer;
using var context = new TestContext(ContextUtil.GetOptions());
var entity = new Item
{
OwnedEntity = new("asd", "asdsd"),
Name = "Name",
Description = "info",
Quantity = 100,
Price = 5,
TimeUpdated = DateTime.Now,
};
context.Items.Add(entity);
await context.BulkSaveChangesAsync();
// Validate Test
int entitiesCount = await context.Items.CountAsync();
Item firstEntity = await context.Items.SingleOrDefaultAsync(a => a.ItemId == 1);
Assert.Equal(1, entitiesCount);
Assert.Equal("asd", firstEntity.OwnedEntity.Mobile);
Assert.Equal("asdsd", firstEntity.OwnedEntity.AlternateMobile);
}
Owned in separate table is not directly supported.
DUPE: #114
Try workaround explained there, also better to use main CRUD Bulk ops instead of BulkSaveChanges.
It seems that when using DbContextBulkTransactionSaveChanges.SaveChangesAsync method the code groups changed entities by EntityType using the Change tracker:

The problem however is that this will also group OwnedEntities (cases where you have an Entity that owns another entity (ValueObject))) per type and causes an object is not an instance error later on when trying to Find the entity type using the context.Model
Code snipper to create an entity with an owned entity:

Entity type configuration:

The text was updated successfully, but these errors were encountered: