![](/img/trans.png)
[英]Serendipity with multiplicity? Composite Primary Key of 2 Foreign Keys in EF throws error
[英]EF - Part of composite keys in foreign key relationships
我正在尝试利用ADO.NET的“数据库中的代码优先”功能。 生成成功,并输出了我所有需要的模型。 我有几种EF模型,它们都具有复合键作为主键。 我在下面给出了2个这样的模型作为示例。
public partial class LabRole
{
public LabRole()
{
LabRolePermissions = new HashSet<LabRolePermission>();
LabUserRoles = new HashSet<LabUserRole>();
RoleSubscriptions = new HashSet<RoleSubscription>();
}
[Key]
[Column(Order = 0)]
[StringLength(50)]
public string RoleID { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(50)]
public string LabID { get; set; }
public virtual ICollection<LabRolePermission> LabRolePermissions { get; set; }
public virtual Lab Lab { get; set; }
public virtual ICollection<LabUserRole> LabUserRoles { get; set; }
public virtual ICollection<RoleSubscription> RoleSubscriptions { get; set; }
}
和:
public partial class LabRolePermission
{
[Key]
[Column(Order = 0)]
[StringLength(50)]
public string LabID { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(50)]
public string RoleID { get; set; }
[Key]
[Column(Order = 2)]
[StringLength(50)]
public string PermissionID { get; set; }
public virtual Lab Lab { get; set; }
public virtual LabRole LabRole { get; set; }
public virtual Permission Permission { get; set; }
}
然后在其上设置了外键关系:
modelBuilder.Entity<LabRole>()
.HasMany(e => e.LabRolePermissions)
.WithRequired(e => e.LabRole)
.HasForeignKey(e => new { e.RoleID, e.LabID })
.WillCascadeOnDelete(false);
我运行add-migration,并成功生成了一个初始迁移脚本。 但是,当我尝试在上下文中访问某些内容时会出错:
Problem in mapping fragments starting at lines 119, 128: Foreign key constraint 'LabRole_LabRolePermissions' from table LabRolePermission (LabID, RoleID) to table LabRole (RoleID, LabID):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.
有人可以解释为什么会这样和/或解决这个问题。
好的,我找到了答案。 如果我不再使用数据注释来指定键,而是使用fluent API来指定键,则生成的数据库将按预期运行。
例如,如果我将模型更改为:
public partial class LabRole
{
public LabRole()
{
LabRolePermissions = new HashSet<LabRolePermission>();
LabUserRoles = new HashSet<LabUserRole>();
RoleSubscriptions = new HashSet<RoleSubscription>();
}
[StringLength(50)]
public string RoleID { get; set; }
[StringLength(50)]
public string LabID { get; set; }
public virtual ICollection<LabRolePermission> LabRolePermissions { get; set; }
public virtual Lab Lab { get; set; }
public virtual ICollection<LabUserRole> LabUserRoles { get; set; }
public virtual ICollection<RoleSubscription> RoleSubscriptions { get; set; }
}
和:
public partial class LabRolePermission
{
[StringLength(50)]
public string LabID { get; set; }
[StringLength(50)]
public string RoleID { get; set; }
[StringLength(50)]
public string PermissionID { get; set; }
public virtual Lab Lab { get; set; }
public virtual LabRole LabRole { get; set; }
public virtual Permission Permission { get; set; }
}
然后使用Fluent即定义键
modelBuilder.Entity<LabRolePermission>().HasKey(e => new { e.LabID, e.RoleID, e.PermissionID });
modelBuilder.Entity<LabRole>().HasKey(e => new { e.RoleID, e.LabID });
我可以毫无问题地访问数据库。 我不太确定为什么会这样,但确实可以。 希望这对以后的人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.