如何首先在Entity Framework 4代码中映射复合主键? [英] How do I map a composite primary key in Entity Framework 4 code first?
本文介绍了如何首先在Entity Framework 4代码中映射复合主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试的配置如下所示:
public SubscriptionUserConfiguration()
{
属性(u => u.SubscriptionID).IsIdentity();
属性(u => u.UserName).IsIdentity();
}
哪个抛出这个异常:
无法推断实体的键
解决方案
你也可以使用
HasKey(u => new {u.SubscriptionID,u.UserName});
编辑:
发现以下情况不起作用:
public ProjectAssignmentConfiguration()
{
HasKey(u = > u.Employee.EmployeeId);
HasKey(u => u.Project.ProjectId);
}
或
public ProjectAssignmentConfiguration()
{
HasKey(u => new {u.Employee.EmployeeId,u.Project.ProjectId});
}
那么如何设置连接表有一个主键的实体由外键组成?
I'm getting to grips with EF4 code first, and liking it so far. But I'm having trouble mapping an entity to a table with a composite primary key.
The configuration I've tried looks like this:
public SubscriptionUserConfiguration()
{
Property(u => u.SubscriptionID).IsIdentity();
Property(u => u.UserName).IsIdentity();
}
Which throws this exception: Unable to infer a key for entity type 'SubscriptionUser'.
What am I missing?
解决方案
You could also use
HasKey(u => new { u.SubscriptionID, u.UserName });
Edit:
One limitation I have found is that the following do not work:
public ProjectAssignmentConfiguration()
{
HasKey(u => u.Employee.EmployeeId);
HasKey(u => u.Project.ProjectId);
}
or
public ProjectAssignmentConfiguration()
{
HasKey(u => new { u.Employee.EmployeeId, u.Project.ProjectId });
}
So how do you set up an entity where the join table has a primary key that is composed of foreign keys?
这篇关于如何首先在Entity Framework 4代码中映射复合主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文