System.Data.SqlClient.SqlException:INSERT语句与FOREIGN KEY约束“FK_dbo.employee_entity_dbo.person_entity_id”冲突。冲突发生在数据库“EFEmployeeEntityModelContext”,表“dbo.person_entity”,列'id'。
因此,当我尝试插入员工实体时,我收到此错误,但我不确定原因。我还有一个人实体类,它与雇员实体有一对一或零关系。所以每个员工都是一个人,但每个人不一定是员工。为了建立这种关系,在我的员工实体类中,我有一个PersonID属性。因此,此PersonID应该是与person实体的Id主键相关的外键。这是我的员工实体类:
[Table("employee_entity")]
public class EFEmployee : EFBusinessEntity
{
[Column("office_id")]
public Guid OfficeID { get; set; }
[Column("person_id")]
//[Index("ix_employee_person_id", IsUnique=true)]
public Guid PersonID { get; set; }
[Column("hire_date")]
public DateTime HireDate { get; set; }
[Column("job_title")]
[Required]
public byte[] JobTitle { get; set; }
[Column("salary")]
public int Salary { get; set; }
[Column("certifications")]
public byte[] Certifications { get; set; }
[Column("vacation_time")]
public int VacationTime { get; set; }
[Column("sick_time")]
public int SickTime { get; set; }
public virtual EFOffice Office { get; set; }
public virtual EFPerson Identity { get; set; }
public virtual EFEmployee ReportingTo { get; set; }
public EFEmployee(Guid id, Guid tenantId, Guid officeID, Guid personID, DateTime hire, byte[] titles, int salary, byte[] certifications, int vacationTime, int sickTime)
{
this.Id = id;
this.TenantId = tenantId;
this.OfficeID = officeID;
this.PersonID = personID;
this.HireDate = hire;
this.JobTitle = titles;
this.Salary = salary;
this.Certifications = certifications;
this.SickTime = sickTime;
this.VacationTime = vacationTime;
}
}
以下是Person实体的类:
[Table("person_entity")]
public class EFPerson : EFBusinessEntity
{
[Column("first_name")]
[StringLength(50)]
public string FirstName { get; set; }
[Column("last_name")]
[StringLength(50)]
public string LastName { get; set; }
[Column("phone_num")]
public uint? PhoneNum { get; set; }
[Column("date_of_birth")]
public DateTime DateOfBirth { get; set; }
public virtual EFEmployee Employee { get; set; }
public EFPerson(Guid id, Guid tenantId, string firstName, string lastName, DateTime dob)
{
this.Id = id;
this.TenantId = tenantId;
this.FirstName = firstName;
this.LastName = lastName;
this.DateOfBirth = dob;
}
}
这是基础实体类:
[Table("business_entity")]
public abstract class EFBusinessEntity : IBusinessEntity
{
[Column("tenant_id")]
public Guid TenantId
{
get;
set;
}
[Column("id")]
[Key]
public Guid Id
{
get;
set;
}
}
以下是我正在运行的测试用例,它为插入员工提供了错误:
public void TestInsertEmployee1()
{
InitializeDatabase();
EFEmployee testEmployee = InitializeEmployee1();
EFRepo.Insert<EFEmployee>(testEmployee);
EFEmployee randomEmployee = EFRepo.GetById<EFEmployee>(testEmployee.Id);
Debug.Assert(testEmployee.Equals(randomEmployee));
}
InitializeEmployee1()创建一个测试员工,当我初始化它时,我确保它使用与调用InitializePerson1()时相同的PersonID。我通过调用
来确保这一点Debug.WriteLine(testEmployee.PersonID);
然后我将它与Microsoft SQL Server中person_entity表中的Id的人物进行比较。如果两个ID匹配,那么问题是什么?
此外,我使用流畅的API在数据库上下文类的OnModelCreating方法中进行映射:
modelBuilder.Entity<EFEmployee>().HasRequired(t => t.Office).WithMany(u => u.Employees).HasForeignKey(d => d.OfficeID);
modelBuilder.Entity<EFEmployee>().HasRequired(t => t.Identity).WithOptional(u => u.Employee);