0
votes

I am trying to use the update-database command on a new table that I am trying to create and I am provided with the following message:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.ActivityLog_dbo.Severity_SeverityLevel". The conflict occurred in database "APGameLogs", table "dbo.Severity", column 'Level'.

Am I doing something wrong? Is this because my foreign key SeverityLevel in the ActivityLog table is a different name than the primary key Level in the Severity table?

My entities are as follows:

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; }

}

My migration is as follows

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");
        }
    }

** This is what the package manager was doing when it failed:**

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

1 Answers

0
votes

You are adding a Default 0 value for [SeverityLevel] in ActivityLog.. You should make sure that you have a [Level] 0 in your Serverity table