问题描述
我有两个code只有POCO的使用EF4和最新的CTP,对现有的,传统的数据库上运行。运行针对PocoA LINQ查询工作,直到我加入了属性下方的对象,我想补充的关系。
I have two "Code Only" POCO's using EF4 and the latest CTP, running against an existing, legacy database. Running a LINQ query against PocoA worked until I added the property below to that object, I was trying to add a relationship.
public virtual PocoB pocoB { get; set; }
在我这样做,我开始收到以下错误:
Once I did that, I started getting the following error:
不支持每个类型的多个对象集。该对象集PocoA_DbSet'和'PocoB_DbSet可以同时包含型PocoA。实例
所以,我接下来想我的问题是因为我还没有确定的关系,而这种传统的数据库使用的是'FK / PK'preFIX,而不是一个'身份证'后缀的主键和外键。所以我增加了以下数据注解上述规定的虚方法,在行为没有改变:
So I next thought my problem was because I had not defined the relationship, and this legacy database was using a 'fk/pk' prefix instead of an 'Id' suffix on the primary and foreign keys. So I added the following data annotation to the virtual method specified above, with no change in behavior:
[RelatedTo(Property="PocoB", ForeignKey="fkPocoB")]
我真的在对什么需要改变,以使这项工作的损失。
I'm really at a loss for what needs to be changed to make this work.
推荐答案
如果你的DbContext类公开出现此错误的多个DbSet< T>其中T出现不止一次性质。基本上它不能找出哪些DbSet类型T的实例属于。
This error occurs if your DbContext class exposes multiple DbSet<T> properties where T occurs more than once. Basically it can't figure out which DbSet an instance of type T belongs to.
在code,误差大概是这样的:
In code, the error probably looked like this:
public class MyContex : DbContext {
public DbSet<PocoA> PocoA { get; set; }
public DbSet<PocoA> PocoB { get; set; } ...
在这里的最后一行应该是DbSet&LT; PocoB&GT;而不是DbSet&LT; PocoA&GT;
where that final line should have been DbSet<PocoB> instead of DbSet<PocoA>
这通常发生在复制和粘贴一行忘记更新在DbSet
这篇关于实体框架4 code仅错误&QUOT;多个对象每种类型设置不支持&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!