EF Codefirst如何创建派生类单独的表? [英] EF Codefirst How to create separate table for derived class?

查看:271
本文介绍了EF Codefirst如何创建派生类单独的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用EF Codefirst自己的表的对象。现在,我尝试生产用于更改的对象居住在为每个对象单独的表存档。

I have objects in their own tables using EF Codefirst. Now I try to produce an "archive" for changed objects living in separate tables for each of those objects.

例如:

public class Person  
{  
    [Key]
    public virtual Guid Id { get; set; }

    [Required]
    public string Name { get; set; }
}

public class Person_Archive : Person 
{
    [Key]
    [Columnn( Order = 1 )]
    public override Guid Id { get; set; }

    [Key]
    [Columnn( Order = 2 )]
    public DateTime ChangedAt { get; set; }

    public string ChangedBy { get; set; }
}

当我让EF创建它的型号不会包含人的Person_Archive :-(即使我添加的属性:

When I let EF create the Model it does NOT include the properties of Person in Person_Archive :-( Even if I add:

modelBuilder.Entity<Person>().ToTable( "Person" );
modelBuilder.Entity<Person_Archiv>().ToTable( "Person_Archiv" );

EF仍无法从派生类重复的属性。

EF still does not repeat the properties from the derived class.

有没有人一个想法如何实现这一目标?

Has anyone an idea how to achieve that?

谢谢!
安德烈亚斯

Thanks! Andreas

推荐答案

是的,你需要调用类似 MapInheritedProperties 的方法来做到这一点。

yeah, you need to call something like MapInheritedProperties method to do that.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Person>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("People");
    });

    modelBuilder.Entity<Person_Archieve>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("People_Archieve");
    });            
}

这篇关于EF Codefirst如何创建派生类单独的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆