Entity inheritance (TPT) with EF6 EntityHistoryHelper
See original GitHub issue.net framework 4.6.2 abp mvc5.x 4.10.1 This problem does not exist in version 4.9
I want to implement a generic tree structure of data on an OrganizationUnit basis Therefore,using EF’s TPT
the following entities are defined:
//Entities ----------------------------------------------------------------------------------------------
public class TreeEntity : OrganizationUnit, IExtendableObject
{
public int OrderIndex { get; set; }
public string ExtensionData { get; set; }
}
[Table("ABPDataDictionaries")]
public class DataDictionaryEntity : TreeEntity
{
public bool IsTree { get; set; }
public bool IsSysDefine { get; set; }
}
//DbContexxt ----------------------------------------------------------------------------------------------
public class ABPDbContext : AbpZeroDbContext<Tenant, Role, User>
{
public virtual DbSet<DataDictionaryEntity> DataDictionaries { get; set; }
}
//General AppService--------------------------------------------------------------------------------------
public abstract class TreeDataAppServiceBase<TEntity, TDto, TEditDto> : ABPAppServiceBase
where TEntity : TreeEntity, IExtendableObject
where TDto : TreeDataBaseDto<TDto>, new()
where TEditDto : TreeNodeEditBaseDto
{
protected readonly OrganizationUnitManager organizationUnitManager;
protected readonly IRepository<TEntity, long> ownRepository;
public TreeDataAppServiceBase(
IRepository<OrganizationUnit, long> repository,
OrganizationUnitManager organizationUnitManager,
IRepository<TEntity, long> ownRepository
{
this.organizationUnitManager = organizationUnitManager;
this.ownRepository = ownRepository;
}
public virtual async Task<TDto> CreateAsync(TEditDto input)
{
if (input.ParentId == 0)
input.ParentId = null;
var m = ObjectMapper.Map<TEntity>(input);
if (input.ExtData != null)
{
foreach (var item in input.ExtData)
{
m.SetData(item.Key, item.Value);
}
}
//分布式环境可能不会达到预期结果,但是对系统影响不大
if (m.OrderIndex == 0)
m.OrderIndex = (await ownRepository.GetAll()
.Where(c => c.ParentId == m.ParentId)
.OrderByDescending(c => c.OrderIndex)
.Select(c => c.OrderIndex)
.FirstOrDefaultAsync()) + 1;
await BeforeCreate(m);
await organizationUnitManager.CreateAsync(m);
await CurrentUnitOfWork.SaveChangesAsync(); //here trow exception
return ObjectMapper.Map<TDto>(m);
}
//other code
appservice subclass:-------------------------------------------------------------------------------------
public class DataDictionaryAppService
: TreeDataAppServiceBase<DataDictionaryEntity, DataDictionaryDto, EditDataDictionaryDto>,
IDataDictionaryAppService
{
public DataDictionaryAppService(
IRepository<OrganizationUnit, long> repository,
OrganizationUnitManager organizationUnitManager,
IRepository<DataDictionaryEntity, long> userOrganizationUnitRepository)
: base(repository, organizationUnitManager, userOrganizationUnitRepository)
{
}
Data table generation OK
when I call DataDictionaryAppService.CreateAsync,I got the following exception note: [sequence matching element does not contain any]=[序列不包含任何匹配元素]
ERROR 2019-11-06 00:44:31,254 [29 ] nHandling.AbpApiExceptionFilterAttribute - 序列不包含任何匹配元素
System.InvalidOperationException: 序列不包含任何匹配元素
在 System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
在 Abp.EntityHistory.EntityHistoryHelper.GetEntitySet(ObjectContext context, EntityType entityType)
在 Abp.EntityHistory.EntityHistoryHelper.CreateEntityChangeSet(DbContext context)
在 Abp.Zero.EntityFramework.AbpZeroCommonDbContext`2.<SaveChangesAsync>d__104.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 Abp.EntityFramework.Uow.EfUnitOfWork.<SaveChangesInDbContextAsync>d__19.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 Abp.EntityFramework.Uow.EfUnitOfWork.<SaveChangesAsync>d__12.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.GetResult()
在 ZLJ.TreeDataAppServiceBase`3.<CreateAsync>d__10.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 Abp.Threading.InternalAsyncHelper.<AwaitTaskWithPostActionAndFinallyAndGetResult>d__5`1.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 Abp.Threading.InternalAsyncHelper.<AwaitTaskWithFinallyAndGetResult>d__3`1.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__1`1.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 Abp.WebApi.Uow.AbpApiUowFilter.<ExecuteActionFilterAsync>d__6.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 Abp.WebApi.Validation.AbpApiValidationFilter.<ExecuteActionFilterAsync>d__5.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 Abp.WebApi.Auditing.AbpApiAuditFilter.<ExecuteActionFilterAsync>d__6.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 Abp.WebApi.Security.AntiForgery.AbpAntiForgeryApiFilter.<ExecuteAuthorizationFilterAsync>d__10.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 Abp.WebApi.Authorization.AbpApiAuthorizeFilter.<ExecuteAuthorizationFilterAsync>d__7.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__6.MoveNext()
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (7 by maintainers)
Top Results From Across the Web
Designer TPT Inheritance - EF6
This step-by-step walkthrough shows how to implement table-per-type (TPT) inheritance in your model using the Entity Framework Designer (EF ...
Read more >Mapping TPT inheritance in Entity Framework 6 (core)
If you configure one entity to map its "Id" property to a column called "FooId", then all entities in the hierarchy will map...
Read more >Inheritance Strategy in EF 6 Code-First
Table per Hierarchy (TPH): This approach suggests one table for the entire class inheritance hierarchy. The table includes a discriminator column which ...
Read more >Table Per Type Inheritance
Table-per-type (TPT) is a mapping strategy used in the object-relational mapping (ORM) to represent inheritance in a relational database.
Read more >ASP.NET Boilerplate changelog - Awesome .NET - LibHunt
PR #5241: Fix Entity History Retrieve the correct owner (by ryancyq) ... ISSUE #4993: Entity inheritance (TPT) with EF6 EntityHistoryHelper ...
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
I tested it with the startup template, here repsitory but the problem is still the same, so prepare to wait for v4.11, thank you!
hi @bxjg1987, Yes v4.11 released and it contains that enhancement too. v4.11.0