AlterColumn在实体框架中对db迁移的索引属性失败

时间:2017-05-23 08:08:25

标签: c# sql-server database entity-framework entity-framework-5

我有以下实体,我已在我的数据库中成功创建了该表。现在,在迁移时,我正在改变“创建过程”。列具有默认值。但每次运行Alter命令时,我都会收到以下错误。

实体

public class MyTeam
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Index("OwnerId_CreatedOn", IsUnique = true, Order = 1)]
    [Required]
    public int OwnerId { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    [Index("OwnerId_CreatedOn", IsUnique = true, Order = 2)]
    [Required]
    [Column(TypeName = "datetime2")]
    public DateTime CreatedOn { get; set; }


    [ForeignKey("OwnerId")]
    public virtual Owner MyOwner { get; set; }

}

在DbMigrations Up方法上更改命令

AlterColumn("MyTeam", "CreatedOn", n => n.DateTime(nullable: false, defaultValueSql: "SYSUTCDATETIME()"));

运行Update-Database时出错

The index 'OwnerId_CreatedOn' is dependent on column 'CreatedOn'.
ALTER TABLE ALTER COLUMN CreatedOn failed because one or more objects access this column.

0 个答案:

没有答案