【问题标题】:Unable to add data into Many-To-Many relationship database table with code first无法先使用代码将数据添加到多对多关系数据库表中
【发布时间】:2015-11-17 13:44:37
【问题描述】:

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

调查

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 表。

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

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

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

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 主键,因为它已经存在于数据库中(表调查)。 我知道它存在,但我只想在用户和调查之间建立关系,而不是创建新的调查或用户。

感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

    您的问题是您提供了一个完整的 UserSurvey 对象,其中填充了嵌套对象(Survey/ApplicationUser)。当您这样做并将变更集设置为“添加”时(由 db.UserSurvey.Add(Item); 引起)。 EF 将尝试为父 (UserSurvey) 和所有子/相关 (Survey/ApplicationUser) 表发出 INSERT INTO,因为它认为“添加”变更集适用于所有这些表。要解决您的问题,您只需提供 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();
    

    因此,如果嵌套记录(本例中的调查/应用程序)已经存在,则基本思想是填充 Id。当您希望 EF 也尝试为您插入嵌套对象时,您只填充了嵌套对象。如果你不想要这个,不要填充它们。仅 Id 可帮助 EF 仅创建关系,而不是为您再次创建这些相关记录。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      相关资源
      最近更新 更多