问题描述
我有两个仅代码POCO使用EF4和最新的CTP,针对现有的遗留数据库运行。对PocoA运行LINQ查询工作,直到我将下面的属性添加到该对象,我试图添加一个关系。 public虚拟PocoB pocoB {get;组;
一旦我这样做,我开始收到以下错误:
不支持每种类型的多个对象集。对象设置PocoA_DbSet和PocoB_DbSet都可以包含类型为PocoA的实例。
所以我接下来认为我的问题是因为我没有定义关系,而这个遗留数据库在主键和外键上使用'fk / pk'前缀而不是'Id'后缀。所以我将以下数据注释添加到上面指定的虚拟方法中,行为没有变化:
[RelatedTo(Property = PocoB,ForeignKey =fkPocoB)]
我真的很失去需要什么
如果您的DbContext类暴露了多个DbSet< T> T发生多于一次的属性。基本上,它无法弄清楚哪个DbSet的类型T的实例属于。
在代码中,错误可能如下所示:
public class MyContex:DbContext {
public DbSet< PocoA> PocoA {get;组; }
public DbSet< PocoA> PocoB {get;组; } ...
哪个最后一行应该是DbSet< PocoB>而不是DbSet< PocoA>
TL; DR - 您复制粘贴了一个属性,而忘记更改类型参数在DbSet中。
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:
Multiple object sets per type are not supported. The object sets 'PocoA_DbSet' and 'PocoB_DbSet' can both contain instances of type 'PocoA'.
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.
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.
In code, the error probably looked like this:
public class MyContex : DbContext {
public DbSet<PocoA> PocoA { get; set; }
public DbSet<PocoA> PocoB { get; set; } ...
where that final line should have been DbSet<PocoB> instead of DbSet<PocoA>
TL;DR - you copy-pasted a property and forgot to change the type parameter in the DbSet.
这篇关于实体框架4仅代码错误“不支持每种类型的多个对象集”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!