EF 6. Multiple added entities may have the same primary key. Error

asp.net c# entity-framework entity-framework-6

Question

I am working on some C# project using Entity Framework 6

I want to update-database but I have this error after 'Running Seed Method'

Unable to determine the principal end of the 'CookerAPI.Models.Category_Recipe_Recipe' relationship. Multiple added entities may have the same primary key.

Here's part of Seed method:

            context.Recipes.AddOrUpdate(x => x.Id_Recipe,
                new Recipe() { Id_Recipe = 1, Id_User = 1, Id_Category_Main = 1, Name_Recipe = "zupa z kurek", Rate = 0, Level = "Łatwe", Date_Recipe = DateTime.Now, URL_Photo = "test", Time = 45, Number_Person = 4, Steps = 4, Instruction = "test" }
                );


            context.Categories_Recipes.AddOrUpdate(x => x.Id_Category_Recipe,
                new Category_Recipe() { Id_Category_Recipe = 1, Id_Recipe = 1, Id_Category = 1 }
                );

Category_Recipe model:

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

    public int Id_Category { get; set; }

    public int Id_Recipe { get; set;}

    [ForeignKey("Id_Category")]
    public Category Category { get; set; }
    [ForeignKey("Id_Recipe")]
    public Recipe Recipe { get; set; }

}

Recipe model:

public class Recipe
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id_Recipe { get; set; }
        public int Id_User { get; set; }
        public int Id_Category_Main { get; set; }

        public string Name_Recipe { get; set; }
        public int Rate { get; set; } //rate 0-5
        public string Level { get; set; }
        public DateTime Date_Recipe { get; set; } //date of create
        public string URL_Photo { get; set; } //URL of thumbnail
        public int Time { get; set; } // in minutes
        public int Number_Person { get; set; } // recipe for number of people
        public int Steps { get; set; }
        public string Instruction { get; set; }

        [ForeignKey("Id_User")]
        public User User { get;set;}
        [ForeignKey("Id_Category_Main")]
        public Category_Main Category_Main { get; set; }

        public ICollection<Category_Recipe> Categories_Recipes { get; set; }
        public ICollection<Comment> Comments { get; set; }
        public ICollection<Element> Elements { get; set; }
        public ICollection<Rate> Rates { get; set; }

    }

Where is the problem and How can I fix it ?

1
0
9/10/2017 2:56:41 PM

Popular Answer

Solution:

context.Recipes.AddOrUpdate(x => x.Id_Recipe,
                new Recipe() { Id_Recipe = 1, Id_User = 1, Id_Category_Main = 1, Name_Recipe = "zupa z kurek", Rate = 0, Level = "Łatwe", Date_Recipe = DateTime.Now, URL_Photo = "test", Time = 45, Number_Person = 4, Steps = 4, Instruction = "test" } );

Replace with:

    context.Categories_Recipes.Add(new Category_Recipe() { Id_Recipe = 3, Id_Category = 3 });
    context.SaveChanges();
0
9/10/2017 4:53:14 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow