简体   繁体   English

如何将第二个外键添加到EF属性中,该属性已经是复合外键的一部分

[英]How to add a second foreign key to an EF property which is part of a composite foreign key already

I am trying to create an audit trail for a table by creating a duplicate of that table which includes an extra date as part of the primary key. 我正在尝试通过创建该表的副本来创建表的审计跟踪,该表包含作为主键一部分的额外日期。

Here is a simplified example of what I want to do. 这是我想要做的简化示例。

我想在一些EF实体中创建的关系图

My code for the entities is currently as follows: 我的实体代码目前如下:

public class Person
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }
}

public class GiftIdea
{
    [Key, Column(Order:0), ForeignKey("Person")]
    public int PersonID { get; set; }
    [Key, Column(Order:1)]
    public DateTime RecordDate { get; set; }
    public string Description { get; set; }
    public string Occasion { get; set; }

    //Linked Entities
    public virtual Person Person { get; set; }
}

public class AuditGiftIdea
{
    [Key, Column(Order:0), ForeignKey("GiftIdea")]
    public int PersonID { get; set; }
    [Key, Column(Order:1), ForeignKey("GiftIdea")]
    public DateTime RecordDate { get; set; }
    [Key, Column(Order:2)]
    public DateTime AuditRecordDate { get; set; }
    public string Description { get; set; }
    public string Occasion { get; set; }

    //Linked Entities
    public virtual GiftIdea GiftIdea { get; set; }
}

Ultimately I want to be able to reference a Person from an AuditGiftIdea object without having to go through the attached GiftIdea. 最终,我希望能够从AuditGiftIdea对象引用Person,而无需通过附加的GiftIdea。

So I tried this: 所以我尝试了这个:

public class AuditGiftIdeas
{
    [Key, Column(Order:0), ForeignKey("GiftIdea"), ForeignKey("Person")] //Extra Foreign Key
    public int PersonID { get; set; }
    [Key, Column(Order:1), ForeignKey("GiftIdea")]
    public DateTime RecordDate { get; set; }
    [Key, Column(Order:2)]
    public DateTime AuditRecordDate { get; set; }
    public string Description { get; set; }
    public string Occasion { get; set; }

    //Linked Entities
    public virtual GiftIdea GiftIdea { get; set; }
    public virtual Person Person { get; set; } //Access to desired object
}

But I get a compile time error of "Duplicate 'ForeignKey' attribute". 但是我得到了“Duplicate'OfternKey'属性的编译时错误”。

How can I accomplish this setup? 我该如何完成此设置?

You cannot do that with data annotations because ForeignKeyAttribute is marked with AllowMultiple = false . 您无法使用数据注释执行此操作,因为ForeignKeyAttribute标记为AllowMultiple = false

But you can configure that with Fluent API: 但您可以使用Fluent API进行配置:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<AuditGiftIdea>()
        .HasRequired(t => t.Person).WithMany()
        .HasForeignKey(t => t.PersonID)
        .WillCascadeOnDelete(false);

    base.OnModelCreating(modelBuilder);
}

Note that you need to turn cascade delete off, because the new FK is creating a circular cascade path and EF will throw an error if you don't do that. 请注意,您需要关闭级联删除,因为新的FK正在创建循环级联路径,如果您不这样做,EF将抛出错误。 Which in turn means you may have a problems with deleting your Person records - you'll have to manually delete the related records from the AuditGiftIdea table. 这反过来意味着您可能在删除Person记录时遇到问题 - 您必须手动从AuditGiftIdea表中删除相关记录。 So think carefully before introducing that FK. 所以在介绍FK之前要仔细考虑。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM