我在两个实体之间存在约束问题,我似乎无法纠正。有人看到我的错误吗?
1。街道地址实体:
public class StreetAddress : BaseEntity
{
// backing fields
private ICollection<Employee> _employees;
public StreetAddress()
{
_employees = new Collection<Employee>();
}
......
// foreign keys
public int Fk_CountryId { get; set; }
public int Fk_StateProvinceId { get; set; }
// associations
/// Country
[ForeignKey("Fk_CountryId")]
public Country Country { get; set; }
/// StateProvince
[ForeignKey("Fk_StateProvinceId")]
public StateProvince StateProvince { get; set; }
// collections
public ICollection<Employee> Employees
{
get { return _employees; }
set { _employees = value; }
}
}
1。员工实体:
public partial class Employee : BaseEntity
{
// backing fields
private ICollection<StreetAddress> _streetAddresses;
public Employee()
{
_streetAddresses = new Collection<StreetAddress>();
}
// foreign keys
[Required]
public string Fk_ApplicationUserId { get; set; }
public int Fk_ClientId { get; set; }
public int Fk_PersonId { get; set; }
#region association
// association
/// application users
[ForeignKey("Fk_ApplicationUserId")]
public ApplicationUser ApplicationUser { get; set; }
/// client company
[ForeignKey("Fk_ClientId")]
public ClientCompany ClientCompany { get; set; }
/// person
[ForeignKey("Fk_PersonId")]
public Person Person { get; set; }
/// region
[ForeignKey("RegionId")]
public virtual Region Region { get; set; }
#endregion association
/// StreetAddresses
public ICollection<StreetAddress> StreetAddresses
{
get { return _streetAddresses; }
set { _streetAddresses = value; }
}
}
}
第3。映射:
modelBuilder.Entity<Employee>()
.HasRequired(e => e.Region)
.WithMany()
.HasForeignKey(r => r.RegionId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Employee>()
.HasRequired(e => e.ClientCompany)
.WithMany()
.HasForeignKey(r => r.Fk_ClientId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Employee>()
.HasRequired(e => e.Person)
.WithMany()
.HasForeignKey(r => r.Fk_PersonId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Employee>()
.HasKey(c => c.Id)
.HasMany(c => c.StreetAddresses)
.WithMany(c => c.Employees)
.Map(c =>
{
c.MapLeftKey("EmployeeId");
c.MapRightKey("AddressId");
c.ToTable("EmployeeLocations");
}
);
modelBuilder.Entity<StreetAddress>()
.HasRequired(e => e.StateProvince)
.WithMany()
.HasForeignKey(r => r.Fk_StateProvinceId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<StreetAddress>()
.HasRequired(e => e.Country)
.WithMany()
.HasForeignKey(r => r.Fk_CountryId)
.WillCascadeOnDelete(false);
}
错误:
在表'EmployeeStreetAddresses'上引入FOREIGN KEY约束'FK_dbo.EmployeeStreetAddresses_dbo.StreetAddresses_StreetAddress_Id'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 无法创建约束。查看以前的错误。
答案 0 :(得分:0)
<强>解决方案:强>
问题在于地址和员工都喜欢与客户建立联系。为了解决这个问题,我绘制了以下内容:
modelBuilder.Entity<ClientCompany>()
.HasKey(c => c.Id)
.HasMany(c => c.StreetAddresses)
.WithRequired(c => c.Client)
.WillCascadeOnDelete(false);
modelBuilder.Entity<ClientCompany>()
.HasKey(c => c.Id)
.HasMany(c => c.Employees)
.WithRequired(c => c.Client)
.WillCascadeOnDelete(false);