【发布时间】:2021-07-27 19:50:19
【问题描述】:
通常应用程序是由 ID 字段构成的,只是在数据库中将其命名为“ID”。但是在我工作的应用程序中不遵循这个约定。我有两个表,它们被规范化为用继承表示。主表称为“Entity”,其主键称为“IdEntity”。而另一个从Entity继承的表叫做“Source”,它的主键叫做“IdSource”,但它不是身份,是Entity.IdEntity的外键。
在当前应用程序(webforms)中它工作正常,我需要用 EF Core 和 .NET Core 5 在新应用程序上表示它。如果我用 _context.Entities.Find(id) 列出它工作正常,但是当我执行 _context.Sources.Find(id) 时,同样不适用。
我的课程定义为:
实体
public class Entity
{
[Column("IdEntity"), Key]
public int Id { get; set; }
}
来源
public class Source : Entity
{
[Column("IdSource"), Key]
public int Id { get; set; }
}
请注意,我在 Source 中明确指定了 ID 列。但是当我对代码运行查询时,我收到错误消息:“Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'IdEntity'.'”
我使用 SQL Server Profiler 跟踪执行,并意识到 EF 正在创建查询,只是忽略 Source 中的列标题 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 */
如何明确告诉 EF Core 主键是 IdSource 而不是 IdEntity for Source?
编辑:这个新应用程序是数据库优先的,它是强制性的!我正在尝试从基于 Webforms 的现有应用程序迁移。
【问题讨论】:
-
Source.IdEntity可能是基于导航属性自动创建的阴影列?还有,部署新应用时不能迁移数据库?
标签: c# entity-framework .net-core entity-framework-core