CodeFirst EF 4.1 Inheritance - Renaming PC / FK

I have two classes: ThreadItem and request.

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 it works, not just one problem. I have a table [ThreadItem] and a table [Request]. In my Query table, there is PK / FK that maps to ThreadItem, and this is what I need. However, I would like to rename the column.

This is currently [ThreadItemId], according to the ThreadItemMapping class. I would like to rename it [InquiryId]. I understand that it is called [ThreadItemId] makes sense, it is rather a question of "is it possible" to be honest.

Any ideas?

Cheers d

+3
source share
1 answer

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

, , , .

Fluent-API, , .

POCO:

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

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

:

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");
}
}

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());
}
}

, MyContext, , EF, .

+2

All Articles