我正在尝试使用代码优先方法和流畅配置使用 EF 4.3.1 设置一个简单的继承场景。
我创建了一个具有一对一导航属性的抽象基类型“A”和一个也具有一对一导航属性的继承类“AA”具有以下内容:
public abstract class A
{
public Guid ID { get; set; }
public B ChildB { get; set; }
}
public class AA : A
{
public C ChildC { get; set; }
}
public class B
{
public Guid ID { get; set; }
public A Parent { get; set; }
}
public class C
{
public Guid ID { get; set; }
public AA Parent { get; set; }
}
public class AConfiguration : EntityTypeConfiguration<A>
{
public AConfiguration()
{
this.HasRequired(o => o.ChildB)
.WithRequiredPrincipal(o => o.Parent);
this.Map(o =>
{
o.ToTable("A");
});
}
}
public class AAConfiguration : EntityTypeConfiguration<AA>
{
public AAConfiguration()
{
this.HasRequired(o => o.ChildC)
.WithRequiredPrincipal(o => o.Parent);
this.Map(o =>
{
o.ToTable("AA");
});
}
}
public class BConfiguration : EntityTypeConfiguration<B>
{
public BConfiguration()
{
this.HasRequired(o => o.Parent)
.WithRequiredDependent(o => o.ChildB);
this.Map(o =>
{
o.ToTable("B");
});
}
}
public class CConfiguration : EntityTypeConfiguration<C>
{
public CConfiguration()
{
this.HasRequired(o => o.Parent)
.WithRequiredDependent(o => o.ChildC);
this.Map(o =>
{
o.ToTable("C");
});
}
}
public class DataContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add<A>(new AConfiguration());
modelBuilder.Configurations.Add<AA>(new AAConfiguration());
modelBuilder.Configurations.Add<B>(new BConfiguration());
modelBuilder.Configurations.Add<C>(new CConfiguration());
}
public DbSet<AA> AASet { get; set; }
public DbSet<B> BSet { get; set; }
public DbSet<C> CSet { get; set; }
}
当我尝试使用第一级导航属性取回我的数据时,它按预期工作:
... dataContext.AASet.Include("ChildB") ...
但是当我尝试包含继承类型的导航属性时,如下所示:
... dataContext.AASet.Include("ChildC") ...
我在运行时收到一个 EntityCommandCompilationException 异常消息:
The ResultType of the specified expression is not compatible with the required type. The expression ResultType is 'Transient.reference[...A]' but the required type is 'Transient.reference[...AA]'. Parameter name: arguments[0]
有没有人遇到过类似的问题?
我可能遗漏了一些东西,但我看不出这个样本有什么问题。
我该怎么做才能让我的模型按预期工作?
最佳答案
不,你不会错过任何东西。实际上你遇到了一个旧的 Entity Framework 错误。你的第二个查询可以这样写:
var result = dataContext.ASet.OfType<AA>().Include("ChildC").ToList();
(当您将 DbSet AASet
替换为 ASet
时)。
对于这种在继承类型上一对一映射子项的预先加载,本文适用:http://weblogs.asp.net/johnkatsiotis/archive/2010/04/28/huge-ef4-inheritance-bug.aspx
这个错误很久以前就在这里报告过:https://connect.microsoft.com/VisualStudio/feedback/details/544639/ef4-inheritance-defined-using-queryview-doesnt-work-properly-with-association
该错误在 EF 4.3.1 中仍然存在。但微软已在此线程中宣布该错误已在 .NET 4.5 (= EF 5.0) 中修复。
如果关系是一对多而不是一对一,则代码可以工作。延迟加载或显式加载也可以(也适用于一对一关系),我相信:
var result = dataContext.ASet.OfType<AA>().ToList();
foreach (var item in result)
dataContext.Entry(item).Reference(a => a.ChildC).Load();
但这会产生多个查询。如果您没有遇到多个查询的性能问题,我更喜欢最后一种解决方法 - 直到您可以迁移到 EF 5.0。
关于c# - EF 4.3.1 在 LinqToEntities 查询中包含继承的导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10705218/