![](/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.