EF:ALTER TABLE语句与FOREIGN KEY约束冲突

时间:2015-10-16 14:00:44

标签: sql sql-server database entity-framework ef-code-first

我正在尝试在我要创建的新表上使用update-database命令,并向我提供以下消息:

  

ALTER TABLE语句与FOREIGN KEY约束冲突   " FK_dbo.ActivityLog_dbo.Severity_SeverityLevel&#34 ;.冲突发生了   在数据库" APGameLogs",table" dbo.Severity",column' Level'。

我做错了吗?这是因为SeverityLevel表中的外键ActivityLogLevel表中的主键Severity的名称不同吗?

我的实体如下:

public class ActivityLog
{
    public int ActivityLogId { get; set; }

    [Required]
    public int ActionLogId { get; set; }

    [Required]
    public int ActivityId { get; set; }

    [Range(0, 7)]
    public int SeverityLevel { get; set; }

    [Required]
    public DateTime ActivityDate { get; set; }

    // Navigation properties
    public ActionLog ActionLog { get; set; }
    public Activity Activity { get; set; }

    [ForeignKey("SeverityLevel")]
    public Severity Severity { get; set; }

}

public class Severity
{
    public Severity()
    {
        this.ActivityLogs = new HashSet<ActivityLog>();
    }

    [Key]
    [Range(0,7)]
    public int Level { get; set; }

    [MaxLength(50)]
    [Required]
    public string Name { get; set; }

    [MaxLength(256)]
    public string Description { get; set; }

    // Navigation properties
    public virtual ICollection<ActivityLog> ActivityLogs { get; set; }

}

我的迁移如下

public partial class Severity : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Severity",
                c => new
                    {
                        Level = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false, maxLength: 50),
                        Description = c.String(maxLength: 256),
                    })
                .PrimaryKey(t => t.Level)
                .Index(t => t.Name, unique: true, name: "UK_Severity_Name");

            AddColumn("dbo.ActivityLog", "SeverityLevel", c => c.Int(nullable: false));
            CreateIndex("dbo.ActivityLog", "SeverityLevel");
            AddForeignKey("dbo.ActivityLog", "SeverityLevel", "dbo.Severity", "Level");
        }

        public override void Down()
        {
            DropForeignKey("dbo.ActivityLog", "SeverityLevel", "dbo.Severity");
            DropIndex("dbo.Severity", "UK_Severity_Name");
            DropIndex("dbo.ActivityLog", new[] { "SeverityLevel" });
            DropColumn("dbo.ActivityLog", "SeverityLevel");
            DropTable("dbo.Severity");
        }
    }

**这是包管理器在失败时正在做的事情:**

Applying explicit migration: 201510161347096_Severity.

CREATE TABLE [dbo].[Severity] (
    [Level] [int] NOT NULL IDENTITY,
    [Name] [nvarchar](50) NOT NULL,
    [Description] [nvarchar](256),
    CONSTRAINT [PK_dbo.Severity] PRIMARY KEY ([Level])
)
CREATE UNIQUE INDEX [UK_Severity_Name] ON [dbo].[Severity]([Name])
ALTER TABLE [dbo].[ActivityLog] ADD [SeverityLevel] [int] NOT NULL DEFAULT 0
CREATE INDEX [IX_SeverityLevel] ON [dbo].[ActivityLog]([SeverityLevel])
ALTER TABLE [dbo].[ActivityLog] ADD CONSTRAINT [FK_dbo.ActivityLog_dbo.Severity_SeverityLevel] FOREIGN KEY ([SeverityLevel]) REFERENCES [dbo].[Severity] ([Level])

1 个答案:

答案 0 :(得分:0)

您要在[SeverityLevel]中为ActivityLog添加默认值0 ..您应该确保[Level]表中有Serverity 0