EF Codefirst 如何为派生类创建单独的表?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您需要调用诸如 MapInheritedProperties 之类的方法来执行此操作。
yeah, you need to call something like
MapInheritedProperties
method to do that.添加此处是为了文档方便...如果您使用 EntityTypeConfiguration 来防止数据模型具有 EF 特定引用,那么您需要使用 EntityTypeConfiguration 的 Map 属性,如下所示。
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.