Entity Framework Code First - 单个实体映射到多个表而无需公共键
在将一个实体映射到现有数据库中的多个表时,我遇到了有关 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果所有表中没有相同的主键,则不可能映射继承。如果要将这些表映射到同一实体,则必须存在一对一关系。这意味着第二个表中的
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 includingStartDate
in the key doesn't make sense.Also TPC inheritance is mapped differently.