EF Codefirst 如何为派生类创建单独的表?

发布于 2024-10-16 01:08:53 字数 786 浏览 2 评论 0原文

我使用 EF Codefirst 在自己的表中拥有对象。现在,我尝试为每个对象的单独表中的已更改对象生成“存档”。

例如:

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 中包含 Person 的属性:-( 即使我添加:

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

EF 仍然不会重复派生类中的属性。

有没有人想法如何实现?

谢谢! 安德烈亚斯

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.

For instance:

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

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 still does not repeat the properties from the derived class.

Has anyone an idea how to achieve that?

Thanks!
Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白馒头 2024-10-23 01:08:53

是的,您需要调用诸如 MapInheritedProperties 之类的方法来执行此操作。

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

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");
    });            
}
余罪 2024-10-23 01:08:53

添加此处是为了文档方便...如果您使用 EntityTypeConfiguration 来防止数据模型具有 EF 特定引用,那么您需要使用 EntityTypeConfiguration 的 Map 属性,如下所示。

public class ArchivedPersonConfiguration : EntityTypeConfiguration<Person_Archieve>
{
    Map(m => m.MapInheritedProperties()).ToTable("People_Archieve");
}

Adding here for documentation sake...if you are using EntityTypeConfiguration to keep your data model from having EF specific references then you need to use Map property of EntityTypeConfiguration like below.

public class ArchivedPersonConfiguration : EntityTypeConfiguration<Person_Archieve>
{
    Map(m => m.MapInheritedProperties()).ToTable("People_Archieve");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文