在表上引入外键约束可能会导致循环或多个级联路径

时间:2015-08-03 20:43:28

标签: c# asp.net asp.net-mvc entity-framework ef-code-first

我已经发现了许多其他问题,但我不确定为什么它会在我的实体上进行。用简单的英文表示。三个实体,账户,网站和合同。一个帐户可以有多个站点。帐户可以有多个合同。一个站点可以有多个合同。因此,在某些情况下,您可以将合同附加到网站,然后将其附加到帐户或直接附加到帐户的合同。

我假设我得到了这个,因为有两个可能的级联删除合同,它是在删除帐户时直接附加到帐户的级联或者在网站是级联时级联删除。

我认为合同中不需要AccountId或SiteId,这不应该触发错误。我希望能够做的是将合同附加到站点(以及随后的帐户)或将合同直接附加到帐户 - 但我仍然希望保持数据库参照完整性。

Introducing FOREIGN KEY constraint 'FK_dbo.Contract_dbo.Site_SiteId' on table 'Contract' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

我的班级/实体:

帐户

public partial class Account
{
    public int Id { get; set; }

    [Display(Name = "Account Name")]
    [Required]
    [MinLength(3, ErrorMessage = "The Account Name should be at least 3 characters")]
    [MaxLength(40, ErrorMessage = "The Account Name should be no more than 40 characters")]
    [Remote("IsAccountNameAvailable", "Helper", ErrorMessage = "This Account name already exists, please try another name")]
    public string AccountName { get; set; }

    [ScaffoldColumn(false)]
    [Display(Name = "Date Account Added")]
    public DateTime DateAdded { get; set; }

    public virtual ICollection<Site> Sites { get; set; }
    public virtual ICollection<Contract> Contracts { get; set; }
}

站点

public partial class Site
{
    public int Id { get; set; }

    [Required(ErrorMessage = "A site must be attached to an account - please specify an account")]
    [Display(Name = "Parent Account")]
    public int AccountId { get; set; }

    [Display(Name = "Site Name")]
    [Required]
    [MinLength(3, ErrorMessage = "The Site Name should be at least 3 characters")]
    [MaxLength(40, ErrorMessage = "The Site Name should be no more than 40 characters")]
    public string SiteName { get; set; }

    [ScaffoldColumn(false)]
    public DateTime DateAdded { get; set; }

    [Required]
    [Display(Name = "Primary Site")]

    //[Remote("IsThereAlreadyAPrimarySite", "Helper", AdditionalFields = "AccountId", ErrorMessage = "There is already a primary site set for this account", HttpMethod = "POST")]
    public bool PrimarySite { get; set; }

    [Display(Name = "Site Notes")]
    [Column(TypeName = "text")]
    public string SiteNotes { get; set; }

    public virtual Account Account { get; set; }

    public virtual ICollection<Contract> Contracts { get; set; }

}

合同

public partial class Contract
{
    public int Id { get; set; }

    public int AccountId { get; set; }
    public int SiteId { get; set; }

    [Display(Name = "Contract Type")]
    [Required]
    public int ContractTypeId { get; set; }

    public virtual Account Account { get; set; }

    public virtual Site Site { get; set; }

    public virtual ContractType ContractType { get; set; }

}

1 个答案:

答案 0 :(得分:0)

DbContext文件覆盖方法OnModelCreating中,必须至少配置一个关系,不要在删除时级联。必须在删除时明确处理该关系

protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
    base.OnModelCreating( modelBuilder );

    modelBuilder.Entity<Contract>()
        .HasRequired( c => c.Site)
        .WithMany( s => s.Contracts )
        .WillCascadeOnDelete( false );
}