当我使用EF6进行插入时,出现此错误
在SET子句中多次指定了列名“ employee_id”。一列不能在同一SET子句中分配多个值。修改SET子句以确保列仅更新一次。如果SET子句更新视图的列,则列名'employee_id'可能在视图定义中出现两次
我的模型如下:
public class Entity
{
public Entity()
{
IsActive = true;
IsDeleted = false;
DateCreated = DateTime.Now;
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long ID { get; set; }
public int CompanyID { get; set; }
public int SubID { get; set; }
public DateTime DateCreated { get; set; }
public bool IsTransient()
{
return EqualityComparer<long>.Default.Equals(ID, default(long));
}
public bool IsDeleted { get; set; }
public bool IsActive { get; set; }
}
public partial class NextOfKin : Entity
{
[Required]
public long employee_id { get; set; }
[StringLength(100)]
[Required]
public string nok_first_name { get; set; }
[StringLength(100)]
[Required]
public string nok_last_name { get; set; }
[StringLength(300)]
[Required]
public string nok_address { get; set; }
[StringLength(100)]
public string nok_email { get; set; }
[StringLength(100)]
public string nok_phone { get; set; }
[StringLength(100)]
public string nok_employer { get; set; }
[StringLength(300)]
public string nok_work_address { get; set; }
[StringLength(100)]
[Required]
public string nok_relationship { get; set; }
public virtual Employee Employee { get; set; }
}
public class Employee : Entity
{
//Person Records
public long UserId { get; set; }
public int TitleId { get; set; }
public int? ReligionId { get; set; }
public string SerialNo { get; set; }
[StringLength(100)]
[Required]
public string FirstName { get; set; }
[StringLength(100)]
[Required]
public string LastName { get; set; }
}
我插入亲戚的代码是这样的。
NextOfKin nextOfKin = new NextOfKin();
nextOfKin.employee_id = newEmployee.ID;
nextOfKin.nok_first_name = "Friday";
nextOfKin.nok_last_name = "Ben";
nextOfKin.nok_address = "XXX";
nextOfKin.nok_email = "xa@xo.com";
nextOfKin.nok_phone = "023938494";
nextOfKin.nok_employer = "50 Queens Street";
nextOfKin.nok_work_address = "51 Queens Street";
nextOfKin.nok_relationship = "Neighbour";
db.NextOfKins.Add(nextOfKin);
db.SaveChanges();
答案 0 :(得分:0)
要解决此问题,请删除近亲模型上的关系,然后进行迁移。
要删除,请从NextOfKin模型中删除public virtual Employee Employee { get; set; }
。
此问题的原因如下:
仅当您正确命名引用属性时,关系才能正确创建。在这种情况下,应使用EmployeeID而不是employee_id来表示近亲和雇员之间的关系。
Employee模型没有指向亲戚模型的链接。如果是一对多,则可以将以下属性添加到Employee模型。
公共虚拟列表NextOfKins {get; set;} //如果您需要延迟加载
或
公开列表NextOfKins {get; set;} //如果您不需要延迟加载