【发布时间】:2018-11-15 12:54:23
【问题描述】:
我们有联系人是通过每个层次结构的表存储的, 联系人可以是公司也可以是个人,而个人始终属于公司。 两者都继承自联系人。
使用的是 EF Core 2.1。
看起来像这样
public abstract class Contact {
public virtual Guid Id { get; set; }
public virtual ICollection<Source> Sources{ set; get; } = new Collection<Source>();
}
public class Company : Contact {
public string CompanyName { set; get; }
public virtual ICollection<Person> People { set; get; } = new Collection<Person>();
}
public class Person: Contact {
public string Name { set; get; }
public DateTime Birthday { set; get; }
public virtual Company Company { set; get; }
}
到目前为止一切顺利,我们现在要做的是查询Sources 并包含contacts(所有这些,无论是个人还是公司)
context.Contact.Include(c => c.Contact).FirstOrDefault();
这会产生以下异常
Unable to cast object of type 'System.DateTime' to type 'System.Nullable``1[System.Guid]'.'
堆栈跟踪
at lambda_method(Closure , ValueBuffer )
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalMixedEntityEntry..ctor(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer& valueBuffer)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.NewInternalEntityEntry(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer& valueBuffer)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTrackingFromQuery(IEntityType baseEntityType, Object entity, ValueBuffer& valueBuffer, ISet`1 handledForeignKeys)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.StartTracking(Object entity, IEntityType entityType)
at lambda_method(Closure , QueryContext , AdSourceCrm , Object[] )
at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler._Include[TEntity](QueryContext queryContext, TEntity entity, Object[] included, Action`3 fixup)
at lambda_method(Closure , TransparentIdentifier`2 )
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
at lambda_method(Closure )
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable`1.GetEnumerator()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities[TOut,TIn](IEnumerable`1 results, QueryContext queryContext, IList`1 entityTrackingInfos, IList`1 entityAccessors)+MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1`1.<CompileQueryCore>b__0(QueryContext qc)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
at EfTest.Program.Main(String[] args) in Program.cs:line 18
我们已经花费了数小时(更像是几天)试图找出原因并进行修复,但无济于事。
EF Core 不知何故与我们的构造发生了冲突(也许构造本身是错误的)
如果您禁用 LazyLoading,问题就会消失。
知道是什么原因造成的,有什么办法可以解决这个问题?
编辑 2
我删除了Sources,因为他们似乎与问题无关
编辑 3 我加了sample repo
只需将 DbContextFactory 中的 ConnectionString 更改为您的本地路径即可。
【问题讨论】:
-
无法重现(EF Core 2.1.4,SqlServer LocalDB)。
-
你使用的是哪个 SqlServer 版本?
-
本地数据库。在 Visual Studio 中的 Sql Server 对象资源管理器中显示 Sql Server 12.0.2000。
-
我添加了一个演示行为的示例 repo
标签: c# entity-framework-core table-per-hierarchy