【发布时间】:2021-07-09 10:57:50
【问题描述】:
我有一个初始类 Employee,它具有两个属性 StateOfEmployment、SecondaryStateOfEmployment 的同一实体,即 StateProvince。
我遇到了这个错误:
{“无法创建‘StateProvince.Employee’之间的关系 和“Employee.SecondaryStateProvinceOfEmployment”,因为有 已经是“StateProvince.Employee”和 'Employee.StateProvinceOfEmployment'。导航属性只能 参与单一关系。”}
我的班级
public partial class Employee
{
public long EmployeeId { get; set; }
public string JobTitle { get; set; }
public string EmployeeCode { get; set; }
public long? CountryOfEmploymentId { get; set; }
public long? StateProvinceOfEmploymentId { get; set; }
public long? SecondaryStateProvinceOfEmploymentId { get; set; }
public string CostCenter { get; set; }
public DateTime StartEffDate { get; set; }
public DateTime CreatedTimestamp { get; set; }
public string CreatedUser { get; set; }
public DateTime UpdatedTimestamp { get; set; }
public string UpdatedUser { get; set; }
public bool IsDeleted { get; set; }
public long ClientId { get; set; }
public long PersonId { get; set; }
public string CityOfEmployment { get; set; }
public DateTime? HireDate { get; set; }
public string BusinessUnit { get; set; }
public string ManagerName { get; set; }
public string ManagerEmail { get; set; }
public long? EmploymentStatusId { get; set; }
public long? EmploymentTypeId { get; set; }
public string EmploymentTypeClientString { get; set; }
public string HomeLegalEntity { get; set; }
public bool Executive { get; set; }
public string JobLevel { get; set; }
public DateTime SysStartTime { get; set; }
public DateTime SysEndTime { get; set; }
public virtual Client Client { get; set; }
public virtual Country CountryOfEmployment { get; set; }
public virtual EmploymentStatus EmploymentStatus { get; set; }
public virtual EmploymentType EmploymentType { get; set; }
public virtual Person Person { get; set; }
public virtual StateProvince StateProvinceOfEmployment { get; set; }
public virtual StateProvince SecondaryStateProvinceOfEmployment { get; set; }
}
我的上下文是
modelBuilder.Entity<Employee>(entity =>
{
entity.Property(e => e.BusinessUnit).HasMaxLength(100);
entity.Property(e => e.CityOfEmployment).HasMaxLength(100);
entity.Property(e => e.CostCenter).HasMaxLength(100);
entity.Property(e => e.CreatedTimestamp).HasDefaultValueSql("(getutcdate())");
entity.Property(e => e.CreatedUser)
.IsRequired()
.HasMaxLength(100)
.HasDefaultValueSql("(suser_sname())");
entity.Property(e => e.EmployeeCode).HasMaxLength(100);
entity.Property(e => e.EmploymentTypeClientString).HasMaxLength(100);
entity.Property(e => e.HomeLegalEntity).HasMaxLength(100);
entity.Property(e => e.JobTitle).HasMaxLength(100);
entity.Property(e => e.ManagerEmail).HasMaxLength(100);
entity.Property(e => e.ManagerName).HasMaxLength(200);
entity.Property(e => e.UpdatedTimestamp).HasDefaultValueSql("(getutcdate())");
entity.Property(e => e.UpdatedUser)
.IsRequired()
.HasMaxLength(100)
.HasDefaultValueSql("(suser_sname())");
entity.HasOne(d => d.Client)
.WithMany(p => p.Employee)
.HasForeignKey(d => d.ClientId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Employee_Client");
entity.HasOne(d => d.CountryOfEmployment)
.WithMany(p => p.Employee)
.HasForeignKey(d => d.CountryOfEmploymentId)
.HasConstraintName("Fk_Employee_CountryId");
entity.HasOne(d => d.EmploymentStatus)
.WithMany(p => p.Employee)
.HasForeignKey(d => d.EmploymentStatusId)
.HasConstraintName("Fk_Employee_EmploymentStatusId");
entity.HasOne(d => d.EmploymentType)
.WithMany(p => p.Employee)
.HasForeignKey(d => d.EmploymentTypeId)
.HasConstraintName("Fk_Employee_EmploymentTypeId");
entity.HasOne(d => d.Person)
.WithMany(p => p.Employee)
.HasForeignKey(d => d.PersonId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Employee_Person");
entity.HasOne(d => d.StateProvinceOfEmployment)
.WithMany(p => p.Employee)
.HasForeignKey(d => d.StateProvinceOfEmploymentId)
.HasConstraintName("Fk_Employee_StateProvinceId");
entity.HasOne(d => d.SecondaryStateProvinceOfEmployment)
.WithMany(p => p.Employee)
.HasForeignKey(d => d.SecondaryStateProvinceOfEmploymentId)
.HasConstraintName("Fk_Employee_SecondaryStateProvinceId");
});
我做错了什么?谢谢。
【问题讨论】:
标签: c# .net entity-framework asp.net-core entity-framework-core