Entity Framework Code First - 单个实体映射到多个表而无需公共键

发布于 2025-01-02 05:44:37 字数 2169 浏览 6 评论 0原文

在将一个实体映射到现有数据库中的多个表时,我遇到了有关 EF 代码第一版本的问题。

Table1: (primaryKey is IdDocument)
----------------------------
IdDocument | CreationDate
----------------------------

Table 2: (primaryKey is on IdDocument and StartDate)
------------------------------------------------------------
IdDocument | StartDate | Label | LastUpdate 
------------------------------------------------------------

在单个实体中,我试图更新数据库中的信息。以下是实体类。

public abstract class DocumentBase
{
    [Column("idDocument")]
    public int? IdDocument { get; set; }
    [Column("CreationDate")]
    public DateTime CreationDate { get; set; }
}

public class Document : DocumentBase
{
    [Column("startDate")]
    public DateTime StartDate { get; set; }
    [Column("lastUpdate")]
    public DateTime LastUpdate { get; set; }
    [Column("label")]
    public string Label { get; set; }
}

以下是同一实体的 DbModelBuilder,

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Document>()
      .Map(m =>
           {
             m.Properties(document => new
                          {
                           document.IdDocument,
                           document.CreationDate
                          });
             m.ToTable("MetaDocument");
           }).Map(m =>
                  {
                   m.Properties(document => new
                                {
                                 document.IdDocument,
                                 document.StartDate,
                                 document.EndDate,
                                 document.Label,
                                 document.LastUpdate,
                                 document.Currency
                                });
                   m.ToTable("Document");
                  }).HasKey(key => new
                            {
                             key.IdDocument, key.StartDate
                            });
    base.OnModelCreating(modelBuilder);
}

它无法更新 Table1。

I am facing an issue regarding EF code first version while mapping one entity to multiple tables in existing database.

Table1: (primaryKey is IdDocument)
----------------------------
IdDocument | CreationDate
----------------------------

Table 2: (primaryKey is on IdDocument and StartDate)
------------------------------------------------------------
IdDocument | StartDate | Label | LastUpdate 
------------------------------------------------------------

In single entity, i was trying to update the information in database. Following is the entity classes.

public abstract class DocumentBase
{
    [Column("idDocument")]
    public int? IdDocument { get; set; }
    [Column("CreationDate")]
    public DateTime CreationDate { get; set; }
}

public class Document : DocumentBase
{
    [Column("startDate")]
    public DateTime StartDate { get; set; }
    [Column("lastUpdate")]
    public DateTime LastUpdate { get; set; }
    [Column("label")]
    public string Label { get; set; }
}

Following is the DbModelBuilder for the same entity,

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Document>()
      .Map(m =>
           {
             m.Properties(document => new
                          {
                           document.IdDocument,
                           document.CreationDate
                          });
             m.ToTable("MetaDocument");
           }).Map(m =>
                  {
                   m.Properties(document => new
                                {
                                 document.IdDocument,
                                 document.StartDate,
                                 document.EndDate,
                                 document.Label,
                                 document.LastUpdate,
                                 document.Currency
                                });
                   m.ToTable("Document");
                  }).HasKey(key => new
                            {
                             key.IdDocument, key.StartDate
                            });
    base.OnModelCreating(modelBuilder);
}

It was failing to update the Table1.

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

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

发布评论

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

评论(1

街角卖回忆 2025-01-09 05:44:37

如果所有表中没有相同的主键,则不可能映射继承。如果要将这些表映射到同一实体,则必须存在一对一关系。这意味着第二个表中的 DocumentId 必须是唯一的,因此在键中包含 StartDate 没有意义。

TPC继承也是映射不同

It is not possible to map inheritance if they don't have same primary key in all tables. If you want to map those tables to the same entity there must be one-to-one relation. It means the DocumentId in the second table must be unique and thus including StartDate in the key doesn't make sense.

Also TPC inheritance is mapped differently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文