无法首先使用代码将数据添加到多对多关系数据库 table

Unable to add data into Many-To-Many relationship database table with code first

我有 3 个 table 连接在一起,如下所示:

调查

public enum Language
    {
        Danish,
        English
    }

    public class Survey
    {
        public Survey()
        {
            Id = Guid.NewGuid();
            DateCreated = DateTime.Now;
            Sort = 0;
        }

        [Key]        
        public Guid Id { get; set; }

        public bool Active { get; set; }

        public string Title { get; set; }

        public bool Status { get; set; }

        [Display(Name = "Kommentar")]
        public string Comment { get; set; }

        public string MAilReciever { get; set; }

        public string CCMailReciever { get; set; }

        public string BBCMailReciever { get; set; }

        public int Sort { get; set; }

        public DateTime DateCreated { get; set; }

        public string StartText { get; set; }

        public string EndText { get; set; }

        public string RegardText { get; set; }

        public Language Language { get; set; }

        public virtual ICollection<UserSurvey> UserSurveys { get; set; }

        public virtual ICollection<Chapters> Chapters { get; set; }

        public virtual ICollection<Questions> Questions { get; set; }
    }

应用程序用户 Mvc 中具有身份的默认 ApplicationUser table。

UserSurvey(我的 Survey 和 ApplicationUser 之间的 relationShipTable)

public class UserSurvey
    {
        [Key, Column(Order = 0)]
        public Guid SurveyId { get; set; }
        [Key, Column(Order = 1)]
        public string ApplicationUserId { get; set; }

        public virtual Survey Survey { get; set; }

        public virtual ApplicationUser ApplicationUser { get; set; }

        public bool Active { get; set; }

        public DateTime StartDate { get; set; }

        public DateTime EndDate { get; set; }

        public string RegardText { get; set; }
    }

我正在尝试通过以下代码在用户和 servey 之间添加新关系:

UserSurvey Item = new UserSurvey();
Item.Survey = s;
Item.ApplicationUser = userinfo;
Item.Active = true;
Item.StartDate = Convert.ToDateTime(dateFrom);
Item.EndDate = Convert.ToDateTime(dateTo);
db.UserSurvey.Add(Item);
db.SaveChanges();

我收到的错误消息说我无法“复制 SurveyId 主键,因为它已经存在于数据库中(table 调查)。 我知道它存在,但我只想在用户和调查之间建立关系,而不是创建新的调查或用户。

感谢您的帮助

您的问题是您提供了一个完整的 UserSurvey 对象,其中填充了嵌套对象 (Survey/ApplicationUser)。当您这样做并将变更集设置为 "Add"(由 db.UserSurvey.Add(Item); 引起)时。 EF 将尝试为父级 (UserSurvey) 和所有 child/related(Survey/ApplicationUser) 表发出 INSERT INTO,因为它认为 "Add" 变更集适用于所有这些表。要解决您的问题,您只需提供 ID。这只会插入一个新的关系:

UserSurvey Item = new UserSurvey();
//Item.Survey = s; //This object filling is making EF believe that you are attempting to add a new record in Survey. Comment it out
//Item.ApplicationUser = userinfo; //This object filling is making EF believe that you are attempting to add a new record in ApplicationUser. Comment it out
Item.ApplicationUserId = userinfo.ApplicationUserId;//or whatever is the id column name in ApplicationUser table/class
Item.SurveyId = s.SurveyId;//I see that you are missing SurveyId property in UserSurvey. You will need to add this. This will be the Foreign Key to Survey table just like you have ApplicationUserId FK to ApplicationUser table.
Item.Active = true;
Item.StartDate = Convert.ToDateTime(dateFrom);
Item.EndDate = Convert.ToDateTime(dateTo);
db.UserSurvey.Add(Item);
db.SaveChanges();

因此,基本思想是如果嵌套记录(在本例中为 Survey/Application)已经存在,则填充 ID。当您希望 EF 也尝试为您插入嵌套对象时,您只填充嵌套对象。如果您不想这样做,请不要填充它们。只是 ID 帮助 EF 只创建关系,而不是在为您再次创建这些相关记录之后再去。

希望对您有所帮助。