C# 实体框架|一个表中的多个对象
我在SQL Server中有一个奇怪的遗留表,我想知道是否可以使用以下逻辑:C# 实体框架|一个表中的多个对象,c#,entity-framework,single-table-inheritance,C#,Entity Framework,Single Table Inheritance,我在SQL Server中有一个奇怪的遗留表,我想知道是否可以使用以下逻辑: 表[PartiesTable] -身份证 -名字 -姓氏 -名字 -类型 以及下列类别: 公共抽象类聚会{ 公共Guid Id{get;set;} } 公众人物:党{ 公共字符串名{get;set;} 公共字符串LastName{get;set;} } 公营公司:党{ 公共字符串名称{get;set;} } 这是一个记录的例子 ╔════╦═════════════╦══════════╦═══════╦═════
表[PartiesTable]
-身份证
-名字
-姓氏
-名字
-类型
以及下列类别:
公共抽象类聚会{
公共Guid Id{get;set;}
}
公众人物:党{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}
公营公司:党{
公共字符串名称{get;set;}
}
这是一个记录的例子
╔════╦═════════════╦══════════╦═══════╦══════╗
║ id ║ FirstName ║ LastName ║ Name ║ Type ║
╠════╬═════════════╬══════════╬═══════╬══════╣
║ 1 ║ John ║ Kenedy ║ NULL ║ P ║
╠════╬═════════════╬══════════╬═══════╬══════╣
║ 2 ║ Meresa ║ Oslo ║ NULL ║ P ║
╠════╬═════════════╬══════════╬═══════╬══════╣
║ 3 ║ NULL ║ NULL ║ ACME ║ C ║
╚════╩═════════════╩══════════╩═══════╩══════╝
我试图按照这里的文档()进行操作,但它们引用了一个示例,其中有3个表,而我没有这些表。这实际上是EF代码中的默认行为。只需定义类,将所有类映射到名为PartiesTable的表,并设置discriminator列。配置应如下所示:
modelBuilder.Entity<Party>()
.Map<Person>(m => { m.Requires("Type").HasValue("P"); m.ToTable("PartiesTable");})
.Map<Company>(m => { m.Requires("Type").HasValue("C"); m.ToTable("PartiesTable"); })
.ToTable("PartiesTable");
modelBuilder.Entity()
.Map(m=>{m.Requires(“Type”).HasValue(“P”);m.ToTable(“PartiesTable”);})
.Map(m=>{m.Requires(“Type”).HasValue(“C”);m.ToTable(“PartiesTable”);})
.不动产(“不动产”);
这实际上是EF代码中的默认行为。只需定义类,将所有类映射到名为PartiesTable的表,并设置discriminator列。配置应如下所示:
modelBuilder.Entity<Party>()
.Map<Person>(m => { m.Requires("Type").HasValue("P"); m.ToTable("PartiesTable");})
.Map<Company>(m => { m.Requires("Type").HasValue("C"); m.ToTable("PartiesTable"); })
.ToTable("PartiesTable");
modelBuilder.Entity()
.Map(m=>{m.Requires(“Type”).HasValue(“P”);m.ToTable(“PartiesTable”);})
.Map(m=>{m.Requires(“Type”).HasValue(“C”);m.ToTable(“PartiesTable”);})
.不动产(“不动产”);