Mar*_*cin 6 c# entity-framework ef-code-first
我在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; }
}
Run Code Online (Sandbox Code Playgroud)
创建表时,它正在创建外键和索引.我想知道为什么,因为这样的索引通常非常低效,而对于大型数据库,它会导致很多问题.那么为什么它只创建该索引而不是外键.以及如何默认禁用此类索引创建.
这只是实体框架的约定。如果您不喜欢它,则可以在项目上启用迁移并将迁移更改为不包含外键。不过,我不同意你的说法,即它效率低下。
要启用数据库迁移,请执行以下操作:
Enable-Migrations
Add-Migration InitialMigration
Up
带有一些语句的方法。找到添加外键的行并将其删除。Update-Database
以应用迁移。这是假设您还没有数据库并且从头开始。