【发布时间】:2019-11-02 09:58:30
【问题描述】:
我使用 SQL Server 2017 (v14.0.2027.2) 和 EF Core 2.1。
在此代码上添加迁移后:
public class TblTrack
{
public long Id { get; set; }
...
}
public class TblProduct
{
public long Id { get; set; }
...
}
public class TblProductItem
{
[Key]
[Required]
public long ProductId { get; set; }
[Key]
[Required]
public long TrackId { get; set; }
// Navigation properties
public TblProduct Product { get; set; }
public TblTrack Track { get; set; }
}
Ef Core 只创建 TrackId 索引
migrationBuilder.CreateIndex(
name: "IX_tbl_ProductItems_TrackId",
table: "tbl_ProductItems",
column: "TrackId");
为什么为TrackId 创建了索引,而不为ProductId 创建了索引?
【问题讨论】:
-
旁注:如果所涉及的两个键列都是 same 数据类型,则 FK/PK 关系最有效 - 您在两个表中的 PK 有
long,但 @987654326 @ 用于引用表中的 FK - 这应该是相同的(两者都int,或两者都long) -
另外:这些关键字段的拼写:你有
ID和TrackID作为生成的密钥(其中ID全是大写),你有Id和@ 987654333@ 你又不一致了 - 一旦你用大写字母拼写,一个是混合大小写......尝试一致 - 要么拼写ID总是 - 要么始终使用Id- 但不要混合 ....
标签: c# sql-server entity-framework entity-framework-migrations ef-core-2.1