Table 每个层次结构和 Table 每个实体框架中的具体类型?
Table per Hierarchy and Table per Concrete type in Enttiy Framework?
我有以下 POCO 模型 类:
//Not mapped to table, has all fields apart from foreign key
public abstract class FirstBaseModel {}
//Mapped to a table, has foreign key (eg. customerId)
public class Derived1Model : FirstBaseModel {}
//Mapped to a different table, has foreign key (eg. companyId)
public class Derived2Model : FirstBaseModel {}
//Mapped to the same table as Derived2Model
public class Derived3Model : Derived2Model {}
在上述情况下,我会混合使用 Table-per-Type 和 Table-per-Hierarchy 继承吗?
TPH 是标准方案中的默认值。
如果您需要 TPT,您只需指定 table 名称。
在你的情况下(我还添加了一些属性)
//Not mapped to table, has all fields apart from foreign key
public abstract class FirstBaseModel
{
public int Id { get; set; }
}
//Mapped to a table, has foreign key (eg. customerId)
[Table("MyTable")]
public class Derived1Model : FirstBaseModel
{
public string D1 { get; set; }
}
//Mapped to a different table, has foreign key (eg. companyId)
[Table("MyDifferentTable")]
public class Derived2Model : FirstBaseModel
{
public string D2 { get; set; }
}
//Mapped to the same table as Derived2Model
[Table("MyDifferentTable")]
public class Derived3Model : Derived2Model
{
public string D3 { get; set; }
}
这是 EF 在数据库上运行的查询
ExecuteNonQuery==========
CREATE TABLE [MyTable] (
[Id] int not null identity(1,1)
, [D1] text null
);
ALTER TABLE [MyTable] ADD CONSTRAINT [PK_MyTable_b9ce81de] PRIMARY KEY ([Id])
ExecuteNonQuery==========
CREATE TABLE [MyDifferentTable] (
[Id] int not null identity(1,1)
, [D2] text null
, [D3] text null
, [Discriminator] varchar(128) not null
);
ALTER TABLE [MyDifferentTable] ADD CONSTRAINT [PK_MyDifferentTable_b9ce81de] PRIMARY KEY ([Id])
我有以下 POCO 模型 类:
//Not mapped to table, has all fields apart from foreign key
public abstract class FirstBaseModel {}
//Mapped to a table, has foreign key (eg. customerId)
public class Derived1Model : FirstBaseModel {}
//Mapped to a different table, has foreign key (eg. companyId)
public class Derived2Model : FirstBaseModel {}
//Mapped to the same table as Derived2Model
public class Derived3Model : Derived2Model {}
在上述情况下,我会混合使用 Table-per-Type 和 Table-per-Hierarchy 继承吗?
TPH 是标准方案中的默认值。
如果您需要 TPT,您只需指定 table 名称。
在你的情况下(我还添加了一些属性)
//Not mapped to table, has all fields apart from foreign key
public abstract class FirstBaseModel
{
public int Id { get; set; }
}
//Mapped to a table, has foreign key (eg. customerId)
[Table("MyTable")]
public class Derived1Model : FirstBaseModel
{
public string D1 { get; set; }
}
//Mapped to a different table, has foreign key (eg. companyId)
[Table("MyDifferentTable")]
public class Derived2Model : FirstBaseModel
{
public string D2 { get; set; }
}
//Mapped to the same table as Derived2Model
[Table("MyDifferentTable")]
public class Derived3Model : Derived2Model
{
public string D3 { get; set; }
}
这是 EF 在数据库上运行的查询
ExecuteNonQuery==========
CREATE TABLE [MyTable] (
[Id] int not null identity(1,1)
, [D1] text null
);
ALTER TABLE [MyTable] ADD CONSTRAINT [PK_MyTable_b9ce81de] PRIMARY KEY ([Id])
ExecuteNonQuery==========
CREATE TABLE [MyDifferentTable] (
[Id] int not null identity(1,1)
, [D2] text null
, [D3] text null
, [Discriminator] varchar(128) not null
);
ALTER TABLE [MyDifferentTable] ADD CONSTRAINT [PK_MyDifferentTable_b9ce81de] PRIMARY KEY ([Id])