简体   繁体   English

EntityFramwork 核心和继承 TPT

[英]EntityFramwork Core and Inheritance TPT

I have read some posts, like this , this and this .我读过一些帖子,比如这个这个这个

Some tables from the database:数据库中的一些表:

在此处输入图片说明

I migrated from EF4 creating the models using Scaffold-DbContext , I expected it generates followings:我从使用Scaffold-DbContext创建模型的 EF4 迁移,我预计它会生成以下内容:

class Tagesinkassos {}
class TagesinkassosPOSTagesinkasso : Tagesinkassos {}
class TagesinkassosTagesinkasso : Tagesinkassos {}

instead I got:相反,我得到了:

class TagesinkassosPOSTagesinkasso {}
class TagesinkassosTagesinkasso {}
class Tagesinkassos {
    public virtual TagesinkassosPOSTagesinkasso TagesinkassosPOSTagesinkasso { get; set; }
    public virtual TagesinkassosTagesinkasso TagesinkassosTagesinkasso { get; set; }
}

I read that TPC is not supported in EFCore yet, but this is TPT, right?我读到 EFCore 尚不支持 TPC,但这是 TPT,对吗?

However, if I modify the generated models, I get:但是,如果我修改生成的模型,我会得到:

System.InvalidOperationException: 'The entity type 'TagesinkassosTagesinkasso' cannot be mapped to a table because it is derived from 'Tagesinkassos'. Only base entity types can be mapped to a table.'

Is it possible to modify the models or is there something in the DB that forces this pattern?是否可以修改模型,或者数据库中是否有强制这种模式的东西?

Table-per-Type isn't supported in EF Core versions lower than 5.0.低于 5.0 的 EF Core 版本不支持Table-per-Type It was first added in EF Core 5 Preview 8 .它最初是在EF Core 5 Preview 8 中添加的 If you want to use TPT you'll have to migrate to EF Core 5.如果要使用 TPT,则必须迁移到 EF Core 5。

Currently EF Core 5 is in RC2 which can be used in production.目前 EF Core 5 处于 RC2 中,可用于生产。 From the announcement :公告

This is a feature complete release candidate of EF Core 5.0 and ships with a "go live" license.这是 EF Core 5.0 的功能完整候选版本,并附带“上线”许可证。 You are supported using it in production.支持在生产中使用它。

From the documentation's example, these classes :从文档的示例中,这些类:

[Table("Animals")]
public class Animal
{
    public int Id { get; set; }
    public string Species { get; set; }
}

[Table("Pets")]
public class Pet : Animal
{
    public string Name { get; set; }
}

[Table("Cats")]
public class Cat : Pet
{
    public string EdcuationLevel { get; set; }
}

[Table("Dogs")]
public class Dog : Pet
{
    public string FavoriteToy { get; set; }
}

Will be mapped to these tables:将映射到这些表:

CREATE TABLE [Animals] (
    [Id] int NOT NULL IDENTITY,
    [Species] nvarchar(max) NULL,
    CONSTRAINT [PK_Animals] PRIMARY KEY ([Id])
);

CREATE TABLE [Pets] (
    [Id] int NOT NULL,
    [Name] nvarchar(max) NULL,
    CONSTRAINT [PK_Pets] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_Pets_Animals_Id] FOREIGN KEY ([Id]) 
               REFERENCES [Animals] ([Id]) ON DELETE NO ACTION
);

CREATE TABLE [Cats] (
    [Id] int NOT NULL,
    [EdcuationLevel] nvarchar(max) NULL,
    CONSTRAINT [PK_Cats] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_Cats_Animals_Id] FOREIGN KEY ([Id]) 
               REFERENCES [Animals] ([Id]) 
               ON DELETE NO ACTION,
    CONSTRAINT [FK_Cats_Pets_Id] FOREIGN KEY ([Id]) 
               REFERENCES [Pets] ([Id]) 
               ON DELETE NO ACTION
);

CREATE TABLE [Dogs] (
    [Id] int NOT NULL,
    [FavoriteToy] nvarchar(max) NULL,
    CONSTRAINT [PK_Dogs] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_Dogs_Animals_Id] FOREIGN KEY ([Id]) 
               REFERENCES [Animals] ([Id]) 
               ON DELETE NO ACTION,
    CONSTRAINT [FK_Dogs_Pets_Id] FOREIGN KEY ([Id]) 
               REFERENCES [Pets] ([Id]) 
               ON DELETE NO ACTION
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM