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.
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.
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".
How can I accomplish this setup?