简体   繁体   English

EF Core 5 TPT - 继承了具有不同 ID 名称的 object

[英]EF Core 5 TPT - Inherited object with different ID name

Usually applications are structured with ID field just naming it as "ID" at the database.通常应用程序是用 ID 字段构造的,只是在数据库中将其命名为“ID”。 But in the application where I'm working doesn't follow this convention.但是在我工作的应用程序中不遵循这个约定。 I have two tables, that is normalized to be represented with inheritance.我有两个表,标准化为用 inheritance 表示。 The main table is called "Entity", and its primary key is called "IdEntity".主表称为“Entity”,其主键称为“IdEntity”。 And the other table, that inherits from Entity is called "Source", and its primary key is called "IdSource", but it isn't identity, is a foreign key to Entity.IdEntity.而另一个从Entity继承的表称为“Source”,其主键称为“IdSource”,但它不是身份,是Entity.IdEntity的外键。

In the current application (webforms) it works fine, and I need to represent it on a new application with EF Core and .NET Core 5. If I list with _context.Entities.Find(id) it works fine, but the same is not applicable when i do _context.Sources.Find(id).在当前应用程序(webforms)中它工作正常,我需要在一个新的应用程序上用 EF Core 和 .NET Core 5 表示它。如果我用 _context.Entities.Find(id) 列出它工作正常,但同样不是当我做 _context.Sources.Find(id) 时适用。

My classes are defined as:我的课程定义为:

Entity实体

public class Entity
{
    [Column("IdEntity"), Key]
    public int Id { get; set; }
}

Source资源

public class Source : Entity
{
    [Column("IdSource"), Key]
    public int Id { get; set; }
}

Note that I'm specifying explicitly the ID column in Source.请注意,我在 Source 中明确指定了 ID 列。 But when I run the query on code, I'm getting the error: "Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'IdEntity'.'"但是,当我对代码运行查询时,我收到错误消息:“Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'IdEntity'.'”

I traced the execution with SQL Server Profiler, and realized that EF is creating the query simply ignoring the Column header IdSource from Source and inserting IdEntity , creating something like this:我使用 SQL Server Profiler 跟踪执行情况,并意识到 EF 正在创建查询,只是忽略了来自 Source 的列 header IdSource并插入IdEntity ,创建如下内容:

select * -- All fields
from Entity t
inner join Source t0 on t.IdEntity = t0.IdEntity 
/* Note that it is inserting t0.IdEntity, but the alias for t0 is for Source, that there are no fields named IdEntity */

How can I explicitly tell to EF Core that the primary key is IdSource and not IdEntity for Source?如何明确告诉 EF Core 主键是 IdSource 而不是 IdEntity for Source?

Edit: this new application is Database-first and it is mandatory.编辑:这个新应用程序是数据库优先的,它是强制性的。 I'm trying to migrate from an existing application based on Webforms.我正在尝试从基于 Webforms 的现有应用程序迁移。

Try using fluent API to configure a model尝试使用流畅的 API配置 model

So that you could do something like this code easily for each entity class:这样您就可以轻松地为每个实体 class 执行类似此代码的操作:

    public class SourceEntityTypeConfiguration : IEntityTypeConfiguration<Source>
    {
        public void Configure(EntityTypeBuilder<Source> builder)
        {
            builder
                .Property(b => b.Id)
                .HasColumnName("IdSource");               
        }
    }

I'm not sure if this question is still relevant for you, but maybe this information will be helpful: https://github.com/dotnet/efcore/issues/19811 .我不确定这个问题是否仍然与您相关,但也许这些信息会有所帮助: https://github.com/dotnet/efcore/issues/19811 Shortly speaking, TPT with different ID-column-names is not possible yet简而言之,具有不同 ID-column-names 的 TPT 尚不可能

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

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