Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架-Fluent API多功能_Entity Framework_Fluent Interface - Fatal编程技术网

Entity framework 实体框架-Fluent API多功能

Entity framework 实体框架-Fluent API多功能,entity-framework,fluent-interface,Entity Framework,Fluent Interface,我在两个实体之间有一个约束问题,我似乎无法纠正。有人看到我的错误了吗 一,。街道地址实体: 一,。员工实体: } 三,。映射: 错误: 在表“EmployeeStreetAddresses”上引入外键约束“FK_dbo.EmployeesReteetAddresses_dbo.StreetAddresses_Id”可能会导致循环或多个级联路径。指定“在删除时不执行操作”或“在更新时不执行操作”,或修改其他外键约束。 无法创建约束。请参阅前面的错误。解决方案: 问题在于地址和员工都喜欢与客户建立联

我在两个实体之间有一个约束问题,我似乎无法纠正。有人看到我的错误了吗

一,。街道地址实体:

一,。员工实体:

}

三,。映射:

错误:

在表“EmployeeStreetAddresses”上引入外键约束“FK_dbo.EmployeesReteetAddresses_dbo.StreetAddresses_Id”可能会导致循环或多个级联路径。指定“在删除时不执行操作”或“在更新时不执行操作”,或修改其他外键约束。 无法创建约束。请参阅前面的错误。

解决方案:

问题在于地址和员工都喜欢与客户建立联系。为了解决这个问题,我映射了以下内容:

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