POCO类定义如下:
public partial class Quoter
{
public int ID { get; set; }
public string Account { get; set; }
public string Password { get; set; }
public string Contactor { get; set; }
public System.DateTime RegisterDt { get; set; }
public int Status { get; set; }
public bool Active { get; set; }
}
public partial class Recycler : Quoter
{
public Recycler()
{
this.ContactInfo = new RecylerContactInfo();
this.CertificatedInfo = new RecylerCertificatedInfo();
this.ShopInfo = new RecylerShopInfo();
this.ClientAppStat = new RecylerClientAppStat();
}
public string Description { get; set; }
public int Region { get; set; }
public bool IsReal { get; set; }
public int Group { get; set; }
public string Re { get; set; }
public Nullable<int> Order { get; set; }
public RecylerContactInfo ContactInfo { get; set; }
public RecylerCertificatedInfo CertificatedInfo { get; set; }
public RecylerShopInfo ShopInfo { get; set; }
public RecylerClientAppStat ClientAppStat { get; set; }
public virtual Region RegionInfo { get; set; }
public virtual RecylerGroup RecylerGroup { get; set; }
public virtual RecylerStat RecylerStat { get; set; }
public virtual RecyclerAccount RecyclerAccount { get; set; }
}public partial class RecyclerAccount
{
public int Recycler { get; set; }
public int Credit { get; set; }
public int Sum { get; set; }
public int Available { get; set; }
public string Guarantee { get; set; }
public virtual Recycler RecyclerInfo { get; set; }
}现在我要查找recycler并且预加载它的导航属性RecyclerAccount,我用下面两只方式尝试均不成功
dbContext.set<Recycler>().include(r=>r.RecyclerAccount).where(r.ID == 1).ToList();
这里抛出的异常信息是:{"指定表达式的 ResultType 与要求的类型不兼容。表达式 ResultType 为“Transient.reference[HuiShou.Quoter]”,但要求的类型为“Transient.reference[HuiShou.Recycler]”。\r\n参数名: arguments[0]"}
我又换成下面的方式查找
dbContext.set<Quoter>().oftype<Recycler>().include(r=>r.RecyclerAccount).where(r=>r.ID == 1).ToList();
这次代码可以顺利执行,但是执行结果发现recycler的导航属性RecyclerAccount没有预加载进来还是为null,看了entityframework生成的sql代码确实没有关联recycleraccount表,只关联了quoter和recycler表这是怎么回事,正确的处理方法是怎么样的啊
public partial class Quoter
{
public int ID { get; set; }
public string Account { get; set; }
public string Password { get; set; }
public string Contactor { get; set; }
public System.DateTime RegisterDt { get; set; }
public int Status { get; set; }
public bool Active { get; set; }
}
public partial class Recycler : Quoter
{
public Recycler()
{
this.ContactInfo = new RecylerContactInfo();
this.CertificatedInfo = new RecylerCertificatedInfo();
this.ShopInfo = new RecylerShopInfo();
this.ClientAppStat = new RecylerClientAppStat();
}
public string Description { get; set; }
public int Region { get; set; }
public bool IsReal { get; set; }
public int Group { get; set; }
public string Re { get; set; }
public Nullable<int> Order { get; set; }
public RecylerContactInfo ContactInfo { get; set; }
public RecylerCertificatedInfo CertificatedInfo { get; set; }
public RecylerShopInfo ShopInfo { get; set; }
public RecylerClientAppStat ClientAppStat { get; set; }
public virtual Region RegionInfo { get; set; }
public virtual RecylerGroup RecylerGroup { get; set; }
public virtual RecylerStat RecylerStat { get; set; }
public virtual RecyclerAccount RecyclerAccount { get; set; }
}public partial class RecyclerAccount
{
public int Recycler { get; set; }
public int Credit { get; set; }
public int Sum { get; set; }
public int Available { get; set; }
public string Guarantee { get; set; }
public virtual Recycler RecyclerInfo { get; set; }
}现在我要查找recycler并且预加载它的导航属性RecyclerAccount,我用下面两只方式尝试均不成功
dbContext.set<Recycler>().include(r=>r.RecyclerAccount).where(r.ID == 1).ToList();
这里抛出的异常信息是:{"指定表达式的 ResultType 与要求的类型不兼容。表达式 ResultType 为“Transient.reference[HuiShou.Quoter]”,但要求的类型为“Transient.reference[HuiShou.Recycler]”。\r\n参数名: arguments[0]"}
我又换成下面的方式查找
dbContext.set<Quoter>().oftype<Recycler>().include(r=>r.RecyclerAccount).where(r=>r.ID == 1).ToList();
这次代码可以顺利执行,但是执行结果发现recycler的导航属性RecyclerAccount没有预加载进来还是为null,看了entityframework生成的sql代码确实没有关联recycleraccount表,只关联了quoter和recycler表这是怎么回事,正确的处理方法是怎么样的啊
解决方案 »
- c#开发插件系统时多个子插件的菜单冲突如何解决!
- C# Win7/Vista C盘下某些目录访问权限问题
- c#存图到数据库
- 100家分店的销售方面的数据传输问题
- 数据库中的varBinary对应C#中的什么类型?怎样使用SqlDataReader读取它?
- 如何判断DataSet是否包含内容,还是空的?
- 请教一个winfrom打包问题
- 请教!新人分不多,一下给了100,所以一下多提几个问题,希望能得到解答,谢谢~
- 请问在C#里面怎样获得应用程序的当前路径
- wrappanel 可以添加window吗
- tlbimp.exe工具转换dll问题
- c#写的一个程序打开两个摄像头,某些电脑有时其中一个打开黑屏,在我的电脑中两个可以正常打开,求解
能详细一点吗,我被这个问题困恼了很久,而且我发现我用dbContext.set<Quoter>.where(q=>q.ID ==1).ToList();返回的结果是ienumable<Recycler>类型的,我现在对entityframework的继承已经觉得是不是有设计上的Bug
partial class BreakAwayContext
{
public DbSet<Quoter> Quoters { get; set; }
public DbSet<Account> Accounts { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>().HasRequired(a => a.RecyclerRef).WithRequiredDependent(r => r.AccountRef);
}
}public abstract class Quoter
{
public int QuoterId { get; set; }
public string Text { get; set; }
}public class Recycler : Quoter
{
public string Description { get; set; }
public Account AccountRef { get; set; }
}public class Account
{
public int AccountId { get; set; }
public Recycler RecyclerRef { get; set; }
}
能简单说一下你业务建模里这3个对象之间的关系吗?不用说明谁是PK/FK,只说明三个对象间的关系即可。我越看越不明白你的需求了。
好的,
Quoter是一个基类,它现在地下有一个派生类交Recycler,这两个类都有对应的数据表,通过TPT方式实现继承,
每个Recycler都一个唯一的RecyclerAccount
http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx
public class BreakAwayContext : DbContext
{
public DbSet<Quoter> Quoters { get; set; }
public DbSet<Account> Accounts { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>()
.HasRequired(a => a.RecyclerRef)
.WithRequiredDependent(r => r.AccountRef);
}
}public abstract class Quoter
{
public int QuoterId { get; set; }
public string Text { get; set; }
}public class Recycler : Quoter
{
public string Description { get; set; }
public Account AccountRef { get; set; }
}public class Account
{
public int AccountId { get; set; }
public Recycler RecyclerRef { get; set; }
}public class PocoExercise
{
static void Main(string[] args)
{
// 这里自己添加一点CUD代码试一下 }
}
modelBuilder.Entity<Account>()
.HasRequired(a => a.QuoterRef)
.WithRequiredDependent(r => r.AccountRef);然后用类似下面这样的查询:
var query = context.Quoters
.Include(q => q.AccountRef)
.OfType<Recycler>()
.Where(r=>r.QuoterId == 1)
.ToList();
ToList去掉了,数据怎么出来? 辛苦你了老兄