我有两个类,类似这样:
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; }
}
我正在尝试使用Entity Framework代码优先将这些映射到数据库模式。架构应如下所示:
+-------------+
| Customer |
+-------------|
| * Id |
| FirstName |
| LastName |
+-------------+
|
|
/|\
+----------------+
| CustomerChange |
+----------------+
| * Id |
| * ChangedAtUtc |
| FirstName |
| LastName |
+----------------+
因此Customer
表显示每个客户的最新状态,每次客户更改时,我们{Customer}表UPDATE
和INSERT
行进入CustomerChange表。 CustomerChange为您提供该客户记录状态的完整历史记录 - 请注意,在此模型中,CustomerChanges应被视为不可变的。
我已尝试在Customer.Id和CustomerChange.ChangedAtUtc属性上添加[Key]
属性,但这不起作用。我已经尝试明确覆盖OnModelCreating:
modelBuilder.Entity<CustomerChange>().Map(m => {
m.MapInheritedProperties();
m.ToTable("CustomerChanges");
}).HasKey(change => new { change.Id, change.ChangedAtUtc });
但我无法使用复合键创建CustomerChanges表(Id,ChangedAtUtc)
(是的,在这个例子中,我依赖于id +时间戳是唯一的。这很好。)
我应该如何装饰/覆盖各种Entity Framework位以正确创建此表?
答案 0 :(得分:1)
我认为这应该有效。将Customer类定义为:
//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);
}
}
将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);
}
}
然后在OnModelCreating
添加映射详细信息public override void OnModelCreating()
{
this.Configurations.Add(new CustomerMap());
this.Configurations.Add(new CustomerChangeMap());
}
我希望它对你有用。