使用实体基类时的 EF CTP5 映射问题
当涉及基类时,我在 EF CTP5 中的多对多关系中遇到了一个奇怪的问题。我将首先向您展示一个有效的简单映射。
我有以下两个类(实体):
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Process> Processes { get; set; }
}
public class Process
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
和我的映射类:
public class ProductMapping : EntityTypeConfiguration<Product>
{
public ProductMapping()
{
ToTable("Products");
HasKey(t => t.ID);
Property(t => t.ID).HasColumnName("product_id");
Property(t => t.Name).HasColumnName("name");
}
}
public class ProcessMapping : EntityTypeConfiguration<Process>
{
public ProcessMapping()
{
ToTable("Processes");
HasKey(t => t.ID);
Property(t => t.ID).HasColumnName("process_id");
Property(t => t.Name).HasColumnName("name");
HasMany(p => p.Products)
.WithMany(p => p.Processes)
.Map(m =>
{
m.ToTable("Product_processes");
m.MapLeftKey(process => process.ID, "process_id");
m.MapRightKey(product => product.ID, "product_id");
});
}
}
该映射工作完美。 但是,我想为我的实体引入一个基类。因此,首先,我创建了以下基类:
public abstract class Entity
{
public int ID { get; set; }
}
我使我的两个实体 Product
和 Process
继承自这个 Entity
基类,当然从每个类中删除了 ID 属性。因此,除了 ID 属性现在在基类中实现之外,这两个实体是相同的。
编译并运行我的项目后,我收到以下“著名的”EF 运行时错误:
“序列包含多个匹配元素”
我知道这个错误与多对多关联有关,因为如果我从 Process
类中删除多对多映射,一切都会运行正确,但当然没有关联。
谁能看出这里有什么问题吗?这是 CTP5 错误还是我的实现有问题?如果事实证明这是一个错误,您有解决方法的建议吗?
I'm having an odd problem with many-to-many relationships in EF CTP5 when a base class is involved. I'll first show you a simple mapping that works.
I have the following two classes (entities):
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Process> Processes { get; set; }
}
public class Process
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
And my mapping classes:
public class ProductMapping : EntityTypeConfiguration<Product>
{
public ProductMapping()
{
ToTable("Products");
HasKey(t => t.ID);
Property(t => t.ID).HasColumnName("product_id");
Property(t => t.Name).HasColumnName("name");
}
}
public class ProcessMapping : EntityTypeConfiguration<Process>
{
public ProcessMapping()
{
ToTable("Processes");
HasKey(t => t.ID);
Property(t => t.ID).HasColumnName("process_id");
Property(t => t.Name).HasColumnName("name");
HasMany(p => p.Products)
.WithMany(p => p.Processes)
.Map(m =>
{
m.ToTable("Product_processes");
m.MapLeftKey(process => process.ID, "process_id");
m.MapRightKey(product => product.ID, "product_id");
});
}
}
This mapping works perfectly.
However, I want to introduce a base class for my entities. So as a start, I created the following base class:
public abstract class Entity
{
public int ID { get; set; }
}
I made my two entities Product
and Process
inherit from this Entity
base class and of course removed the ID property from each class. So the two entities are identical except the ID property is now implemented in the base class.
After compiling and running my project, I get the following "famous" EF runtime error:
"Sequence contains more than one matching element"
I know that this error is related to the many-to-many association, because if I remove the many-to-many mapping from the Process
class, everything runs correctly, but without the association of course.
Can anyone see what the problem is here? Is this a CTP5 bug or is there something wrong in my implementation? If it turns out to be a bug, do you have a suggestion for a workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,只是多对一关联。请参阅这个答案
简而言之:在修复此问题之前不要使用基实体类;您可以使用接口代替 Id 属性。
I ran into the same problem, only with a many-to-one association. See this answer
In a nutshell: don't use a base entity class until this is fixed; you can use an interface instead for the Id property.