Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DbContextBulkTransactionSaveChanges.SaveChangesAsync outputs object not an instance of reference error with Owned entities #787

Closed
ChristopherVR opened this issue Apr 3, 2022 · 3 comments
Labels

Comments

@ChristopherVR
Copy link
Contributor

It seems that when using DbContextBulkTransactionSaveChanges.SaveChangesAsync method the code groups changed entities by EntityType using the Change tracker:
image

image

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

image

Code snipper to create an entity with an owned entity:
image

Entity type configuration:
image

@borisdj
Copy link
Owner

borisdj commented Apr 14, 2022

Can you write entire test for this.

@ChristopherVR
Copy link
Contributor Author

ChristopherVR commented Apr 18, 2022

@borisdj ,

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);
        }

Error being produced:
image

@borisdj
Copy link
Owner

borisdj commented May 11, 2022

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.

@borisdj borisdj closed this as completed May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants