【问题标题】:Entity Framework Add-Migration cannot detect changes that I made on foreign keyEntity Framework Add-Migration 无法检测到我对外键所做的更改
【发布时间】:2015-12-19 00:17:51
【问题描述】:

我正在使用实体框架代码优先方法。当我将新表添加到我的上下文中时,我能够使用 Add-Migration 自动检测。但是,当我在其中一个模型上更改外键时,Add-Migration 无法检测到更改并生成代码来更新外键。

这是我的模型

public class PhoneInfo
{
    [Key]
    [Column(Order = 2)]
    public String ID { get; set; }
    [Required]
    [Key]
    [ForeignKey("Contact"), Column(Order = 1)]
    public String UserID { get; set; }
    [Required]
    [Key]
    [ForeignKey("Contact"), Column(Order = 0)]
    public String ContactID { get; set; }
    public String Value { get; set; }
    public DateTime LastChanged { get; set; }
    public String Type { get; set; }
    public int Order { get; set; }
    public bool IsDeleted { get; set; }
    public DateTime DeletedDate { get; set; }
    public virtual Contact Contact { get; set; }
}

我正在更新下面的 Column(Order = 1)

[ForeignKey("Contact"), Column(Order = 1)]

有什么方法可以让 Add-Migration 自动生成更新数据库所需的代码?

【问题讨论】:

    标签: entity-framework ef-code-first foreign-keys entity-framework-6 entity-framework-migrations


    【解决方案1】:

    对于迁移,您需要在包管理器控制台中执行两个命令:

    1. Add-Migration AddOrder1ContactFK
    2. Update-Database
    

    【讨论】:

    • 我已经这样做了。但我认为由于我执行 Add-Migration 命令时自动生成的 DbMigration 类中的 Up() 和 Down() 方法为空,因此 Update-Database 不会做任何事情。
    【解决方案2】:

    将 AutomaticMigrationsEnabled 属性设置为 true,该属性在 Configuration.cs 的构造函数中定义。首次启用迁移时,其默认值为 false。然后重建您的解决方案并再次尝试 update-database。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-20
      • 2017-05-23
      • 2012-07-04
      • 1970-01-01
      • 2017-09-29
      • 2015-01-15
      • 2014-07-28
      • 2018-02-01
      相关资源
      最近更新 更多