【发布时间】: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