无法首先使用代码将数据添加到“多对多关系”数据库表中

时间:2015-11-17 13:44:38

标签: asp.net-mvc entity-framework code-first

我有3个表连接在一起如下:

调查

0 9,21 * * 0/2 task in the cron file

ApplicationUser 来自Mvc的默认ApplicationUser表,带有标识。

UserSurvey (我在Survey和ApplicationUser之间的关系表)

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

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

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

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

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您的问题是您正在提供已填充嵌套对象(Survey / ApplicationUser)的已完成UserSurvey对象。当你这样做并将变更集设置为&#34;添加&#34; (由db.UserSurvey.Add(Item);)引起。 EF将尝试为父(UserSurvey)和所有子/相关(Survey / ApplicationUser)表发出INSERT INTO,因为它认为&#34;添加&#34; changeset适用于所有这些表。要解决您的问题,您只需提供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为您尝试插入它们时,您只填充了嵌套对象。如果你不想要这个,请不要填充它们。只是Ids帮助EF创建关系,而不是再为你创建这些相关记录。

希望这有帮助。