EntityHistory doesn't support Owned Entity with Multiple FKs
See original GitHub issueAbp version: 5.1.0 (tested on up-to-date dev branch) Target framework: NET Core 3.1 Stack trace:
System.NullReferenceException : Object reference not set to an instance of an object.
at Abp.EntityHistory.EntityHistoryHelper.CreateEntityChangeSet(ICollection`1 entityEntries) in D:\git\aspnetboilerplate\src\Abp.ZeroCore.EntityFrameworkCore\EntityHistory\EntityHistoryHelper.cs:line 83
at Abp.Zero.EntityFrameworkCore.AbpZeroCommonDbContext`3.SaveChanges() in D:\git\aspnetboilerplate\src\Abp.ZeroCore.EntityFrameworkCore\Zero\EntityFrameworkCore\AbpZeroCommonDbContext.cs:line 157
at Abp.EntityFrameworkCore.Repositories.EfCoreRepositoryBase`3.InsertAndGetId(TEntity entity) in D:\git\aspnetboilerplate\src\Abp.EntityFrameworkCore\EntityFrameworkCore\Repositories\EfCoreRepositoryBaseOfTEntityAndTPrimaryKey.cs:line 168
at Castle.Proxies.Invocations.IRepository`2_InsertAndGetId_12.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) in D:\git\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 67
at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformUow(IInvocation invocation, UnitOfWorkOptions options) in D:\git\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 59
at Abp.Domain.Uow.UnitOfWorkInterceptor.Intercept(IInvocation invocation) in D:\git\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkInterceptor.cs:line 48
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IRepository`1Proxy_6.InsertAndGetId(Foo entity)
at Abp.Zero.EntityHistory.SimpleEntityHistory_Test.<>c__DisplayClass8_0.<Should_Write_History_For_Owned_Entities>b__0() in D:\git\aspnetboilerplate\test\Abp.ZeroCore.Tests\Zero\EntityHistory\SimpleEntityHistory_Test.cs:line 72
at Abp.TestBase.AbpIntegratedTestBase`1.WithUnitOfWork(Action action, UnitOfWorkOptions options) in D:\git\aspnetboilerplate\src\Abp.TestBase\TestBase\AbpIntegratedTestBase.cs:line 154
at Abp.Zero.EntityHistory.SimpleEntityHistory_Test.Should_Write_History_For_Owned_Entities() in D:\git\aspnetboilerplate\test\Abp.ZeroCore.Tests\Zero\EntityHistory\SimpleEntityHistory_Test.cs:line 62
Test classes:
[Audited]
public class Foo : Entity
{
public Bar Bar { get; set; }
}
[Owned]
public class Bar
{
public int FooId { get; set; }
public int? AnotherFooId { get; set; }
}
DbContext configuration:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ConfigurePersistedGrantEntity();
modelBuilder.Entity<Foo>(b =>
{
b.OwnsOne(f => f.Bar, a =>
{
a.WithOwner().HasForeignKey(f => f.FooId);
a.HasOne<Foo>()
.WithOne()
.HasForeignKey<Bar>(bar => bar.AnotherFooId)
.IsRequired(false)
.OnDelete(DeleteBehavior.Cascade);
});
});
}
Test case:
WithUnitOfWork(() =>
{
var foo = new Foo
{
Bar = new Bar()
};
_fooRepository.InsertAndGetId(foo);
});
Decorating Bar
with DisableAuditedAttribute
or removing FK configuration makes the test method run without exceptions: looks like that issue is related to auditing FKs of Owned type.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
EntityHistory fails for owned type with reference to other ...
The foreign key property that I want to be at first position is a shadow property - exists only in the change tracker...
Read more >Owned Entity Types - EF Core
Instances of owned entity types cannot be shared by multiple owners (this is a well-known scenario for value objects that cannot be implemented ......
Read more >Changing Foreign Keys and Navigations - EF Core
How to change relationships between entities by manipulating foreign keys and navigations.
Read more >ASP.NET Boilerplate changelog - Awesome .NET - LibHunt
(by maliming); ISSUE #5234: EntityHistory doesn't support Owned Entity with Multiple FKs; ISSUE #5232: Incorrect JavaScript for deleteEntity SweetAlert ...
Read more >Auditing with JPA, Hibernate, and Spring Data JPA
This article demonstrates three approaches to introducing auditing into an application: JPA, Hibernate Envers, and Spring Data JPA.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @maliming, I will take over this issue and read about ef core documentation to see if we should implement a fix for this
According to the configuration of 4nonym0us ,
foreignKey.PrincipalToDependent
will be null. So I suggest that he modify the configuration code.Do you think we should modify the code to be compatible with these cases?
https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f3f376544fb5e43f104270d81692a3c1e0eddc2c/src/Abp.ZeroCore.EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs#L83