简体   繁体   English

如何使用Entity Framework代码优先映射带有复合键的继承表?

[英]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.

相关问题 EF代码优先如何使用复合键从表中读取A - EF Code-First How To Read A From Table With A Composite Key 实体框架代码优先的多对多表外键 - Entity Framework code-first foreign key to many to many table 首先使用Entity Framework代码创建复杂的主键 - Create a complex Primary Key using Entity Framework code-first 在Code-First Entity Framework中包含复合主键中的外键 - Include foreign key in composite primary key in Code-First Entity Framework 如何在实体框架中映射CONTAINSTABLE函数(代码优先)? - How to map CONTAINSTABLE function in Entity Framework (code-first)? 如何使用代码优先的实体框架指定外键 - How to specify a foreign key with code-first Entity Framework 如何在Entity Framework代码优先中设置空外键 - How to set null foreign key in Entity Framework code-first 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 使用实体框架Code-First创建表,运行时 - Create Table, Run Time using entity framework Code-First 一键至二键和一键至第三键Entity Framework 6代码优先 - Composite key one to second and one to third Entity Framework 6 code-first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM