我在EF 6代码中有简单的表定义 - 首先使用简单的外键。
public class Address
{
/// <summary>
/// Gets or sets the id.
/// </summary>
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order = 1)]
public int Id { get; set; }
/// <summary>
/// Gets or sets the town.
/// </summary>
public virtual Town Town { get; set; }
/// <summary>
/// Gets or sets the paf address town id.
/// </summary>
[Column(Order = 2)]
public int TownId { get; set; }
}
创建表时,它会创建外键和索引。我想知道为什么,因为这样的索引通常非常低效,而对于大型数据库,它会导致很多问题。那么为什么它只创建该索引而不是外键。以及如何默认禁用此类索引创建。
答案 0 :(得分:1)
这只是实体框架的惯例。如果您不喜欢它,则可以在项目上启用迁移并将迁移更改为不包括外键。我不同意你的断言,即效率低下。
要启用数据库迁移,请执行以下操作:
Enable-Migrations
Add-Migration InitialMigration
Up
方法。找到添加外键的行并将其删除。Update-Database
以应用迁移。这假设您还没有数据库并且从头开始。