0

I have a constraint problem between two entities that I cannot seem to correct. Anyone see my error?

1. Street Address entity:

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. Employee entity:

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. Mappings:

       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);
    }

Error:

Introducing FOREIGN KEY constraint 'FK_dbo.EmployeeStreetAddresses_dbo.StreetAddresses_StreetAddress_Id' on table 'EmployeeStreetAddresses' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.

1 Answers1

0

Solution:

The problem lies with addresses and employees both enjoying an association with client. To solve this I mapped the following:

        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);