[英]How do I map an inherited table with a composite key using Entity Framework code-first?
I have two classes, something like this: 我有两个课,像这样:
public class Customer {
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class CustomerChange : Customer {
public DateTime ChangedAtUtc { get; set; }
}
I'm trying to map these to a database schema using Entity Framework code-first. 我正在尝试使用Entity Framework代码优先将它们映射到数据库架构。 The schema should look like this:
模式应如下所示:
+-------------+
| Customer |
+-------------|
| * Id |
| FirstName |
| LastName |
+-------------+
|
|
/|\
+----------------+
| CustomerChange |
+----------------+
| * Id |
| * ChangedAtUtc |
| FirstName |
| LastName |
+----------------+
So the Customer
table shows you the LATEST state of every customer, and every time the customer changes, we UPDATE
the Customer table and INSERT
a row into the CustomerChange table. 因此,
Customer
表列出了每一个客户的最新状态,每一次客户的变化,我们UPDATE
Customer表和INSERT
一行到CustomerChange表。 CustomerChange gives you a complete history of the state of that Customer record over time - note that CustomerChanges should be considered immutable in this model. CustomerChange为您提供一段时间内该客户记录状态的完整历史记录-请注意,在此模型中,CustomerChanges应该被认为是不变的。
I've tried putting a [Key]
attribute on both the Customer.Id and the CustomerChange.ChangedAtUtc properties, but that didn't work. 我尝试在
[Key]
.Customer属性和[CustomerChange.ChangedAtUtc [Key]
属性上都放置[Key]
属性,但这没有用。 I've tried explicitly overriding OnModelCreating: 我试过显式重写OnModelCreating:
modelBuilder.Entity<CustomerChange>().Map(m => {
m.MapInheritedProperties();
m.ToTable("CustomerChanges");
}).HasKey(change => new { change.Id, change.ChangedAtUtc });
but I cannot get EF to create the CustomerChanges table with the composite key (Id, ChangedAtUtc) 但我无法让EF使用复合键(Id,ChangedAtUtc)创建CustomerChanges表
(Yes, in this example, I'm relying on the id+timestamp being unique. This is fine.) (是的,在此示例中,我依靠id + timestamp是唯一的。这很好。)
How should I decorate/override the various Entity Framework bits to create this table correctly? 我应该如何修饰/覆盖各种实体框架位以正确创建此表?
I think this should work. 我认为这应该有效。 Defince Customer class as:
将客户类别定义为:
//Customer class
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
//Relationships
public virtual ICollection<CustomerChange> CustomerChanges { get; set; }
}
//Mapping details for Customer
public class CustomerMap : EntityTypeConfiguration<Customer>
{
public CustomerMap()
{
this.HasKey(c => c.Id);
}
}
Defince CustomerChange as: 将CustomerChange定义为:
//CustomerChange class
public class CustomerChange
{
public int Id { get; set; }
public DateTime ChangeDate { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
//Relationships
public virtual Customer Customer { get; set; }
}
//Mapping details for CustomerChange class
public class CustomerChangeMap : EntityTypeConfiguration<CustomerChange>
{
public CustomerChangeMap()
{
this.HasKey(c => new { c.Id, c.ChangeDate });
//Relationship mappings
this.HasRequired(cm => cm.Customer)
.WithMany(c => c.CustomerChanges)
.HasForeignKey(cm => cm.Id);
}
}
Then add mapping details at OnModelCreating 然后在OnModelCreating中添加映射详细信息
public override void OnModelCreating()
{
this.Configurations.Add(new CustomerMap());
this.Configurations.Add(new CustomerChangeMap());
}
I hope it will work for you. 希望它对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.