简体   繁体   English

EF-外键关系中组合键的一部分

[英]EF - Part of composite keys in foreign key relationships

I am attempting to utilise the "Code first from database" feature of ADO.NET. 我正在尝试利用ADO.NET的“数据库中的代码优先”功能。 The generation works successfully and outputs all of my desired models. 生成成功,并输出了我所有需要的模型。 I have several EF models that both have composite keys as primary keys. 我有几种EF模型,它们都具有复合键作为主键。 I've given 2 such models as an example below. 我在下面给出了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; }
}

And: 和:

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; }
}

This then has the foreign key relationship set on it: 然后在其上设置了外键关系:

modelBuilder.Entity<LabRole>()
            .HasMany(e => e.LabRolePermissions)
            .WithRequired(e => e.LabRole)
            .HasForeignKey(e => new { e.RoleID, e.LabID })
            .WillCascadeOnDelete(false);

I run add-migration and an initial migration script is generated successfully. 我运行add-migration,并成功生成了一个初始迁移脚本。 However, when I try and access something in the context it errors: 但是,当我尝试在上下文中访问某些内容时会出错:

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.

Could someone please explain why this is happening and/or the solution to this problem. 有人可以解释为什么会这样和/或解决这个问题。

OK I have found the answer. 好的,我找到了答案。 If I remove the use of data annotations for specifying keys and instead specify them using fluent API the generated database functions as expected. 如果我不再使用数据注释来指定键,而是使用fluent API来指定键,则生成的数据库将按预期运行。

For example, if I change my models to: 例如,如果我将模型更改为:

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; }
}

And: 和:

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; }
}

Then define the keys using Fluent ie 然后使用Fluent即定义键

modelBuilder.Entity<LabRolePermission>().HasKey(e => new { e.LabID, e.RoleID, e.PermissionID });

modelBuilder.Entity<LabRole>().HasKey(e => new { e.RoleID, e.LabID });

I can access the database without any issues. 我可以毫无问题地访问数据库。 I'm not really sure why this works but it does. 我不太确定为什么会这样,但确实可以。 Hope this helps someone in the future. 希望这对以后的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 偶然与多重? EF中2个外键的复合主键引发错误 - Serendipity with multiplicity? Composite Primary Key of 2 Foreign Keys in EF throws error 如何将第二个外键添加到EF属性中,该属性已经是复合外键的一部分 - How to add a second foreign key to an EF property which is part of a composite foreign key already 复合外键是复合主键的一部分 - Composite Foreign Key a part of the Composite Primary Key EntityFramework-复合键和外键 - EntityFramework - Composite keys and foreign key 在EF中使用复合主键作为外键 - Use Composite Primary key as Foreign key in EF EF核心:使用链接属性为组合PK的一部分定义外键 - EF Core: Define foreign key for part of a composite PK using a chained property ASP.NET EF CORE:由两个外键组成的复合键无法正确评估 - ASP.NET EF CORE: Composite key composed of two foreign keys not evaluating properly 如何使用EF fluentApi并设置具有2个不同外键的复合键的类? - How to use EF fluentApi and set a class with composite key of 2 different foreign keys? 实体框架:外键作为组合键的一部分 - Entity Framework:Foreign Key as part of Composite key EF-如何使(复合)外键成为可选 - EF - How to make (composite) foreign key optional
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM