繁体   English   中英

EF 6.多个添加的实体可能具有相同的主键。 错误

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

我正在使用Entity Framework 6进行某些C#项目

我想更新数据库,但在“运行种子方法”后出现此错误

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

这是Seed方法的一部分:

            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模型:

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

}

配方型号:

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

    }

问题出在哪里,我该如何解决?

解:

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.Add(new Category_Recipe() { Id_Recipe = 3, Id_Category = 3 });
    context.SaveChanges();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM