简体   繁体   English

CodeFirst EF 4.1 Inheritance - 重命名 PK/FK

[英]CodeFirst EF 4.1 Inheritance - Renaming PK/FK

I have two classes, ThreadItem and Enquiry.我有两个类,ThreadItem 和 Enquiry。

public class ThreadItem
{
    [Key]
    public int Id { get; set; }

    public DateTime Created { get; set; }
} 

public class ThreadItemMapping : EntityTypeConfiguration<ThreadItem>
{
    public ThreadItemMapping()
    {
        Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("ThreadItemId");
    }
}

[Table("Enquiry")]
public class Enquiry : ThreadItem
{
    public string Comment { get; set;}
}

Now, this works, not a single problem.现在,这行得通,没有一个问题。 I have a [ThreadItem] table, and an [Enquiry] table.我有一个 [ThreadItem] 表和一个 [Enquiry] 表。 My Enquiry table has a PK/FK which is mapped to ThreadItem, which is what I need.我的查询表有一个映射到 ThreadItem 的 PK/FK,这是我需要的。 However, I would like to rename the column.但是,我想重命名该列。

Currently is it [ThreadItemId], as per the ThreadItemMapping class.当前是 [ThreadItemId],根据 ThreadItemMapping class。 I Would like to re-name it [EnquiryId].我想将其重新命名为 [EnquiryId]。 I understand that it being called [ThreadItemId] makes sense, this is more of a 'is it possible' question to be honest.我知道它被称为 [ThreadItemId] 是有道理的,老实说,这更像是一个“是否可能”的问题。

Any ideas?有任何想法吗?

Cheers, D干杯,D

http://msdn.microsoft.com/en-us/library/gg197525%28v=vs.103%29.aspx http://msdn.microsoft.com/en-us/library/gg197525%28v=vs.103%29.aspx

I'm not saying it cannot be done, but these examples don't show such a thing being done.我并不是说它不能完成,但这些例子并没有表明正在做这样的事情。

If you use Fluent-API conventions, however, it's quite simple.但是,如果您使用 Fluent-API 约定,则非常简单。

Your POCO definitions:您的 POCO 定义:

public class BaseClass
{
    public int Id {get; set;}
}

public class DerivedClass : BaseClass
{
    public string Data {get; set;}
}

Configuring the mappings to the DB:配置到 DB 的映射:

public class BaseConfiguration : EntityTypeConfiguration<BaseClass>
{
    public BaseConfiguration()
{
    HasKey(k => k.Id);
    Property(p => p.Id).HasColumnName("Id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).IsRequired();
}
}

public class DerivedConfiguration : BaseConfiguration
{
    public DerivedConfiguration()
{
    Property(p => p.Id).HasColumnName("BaseClassId");
}
}

putting it all together把它们放在一起

public class MyContext : DbContext
{
    public MyContext(string connectionString) : base(connectionString)
{
    Database.SetInitializer<MyContext>(null);
}

public DbSet<BaseClass> Bases { get; set; }
public DbSet<DerivedClass> Deriveds { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new BaseConfiguration());
    modelBuilder.Configurations.Add(new DerivedConfiguration());
}
}

So you instantiate a MyContext passing in your connection string and the configurations tell EF how to relate to the tables.所以你实例化一个 MyContext 传入你的连接字符串,配置告诉 EF 如何与表相关联。

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

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