【发布时间】:2015-08-20 09:12:12
【问题描述】:
我是ASP .NET 的新手。我正在尝试使用以下模型进行简单的 CRUD。 Paper类中有三个ChartOfAccountIds,所以有三个Parent-Child关系。这些关系之一ChartOfAccountIdInventory 导致以下异常:
Introducing FOREIGN KEY constraint FK_dbo.Papers_dbo.ChartOfAccounts_ChartOfAccountIdInventory' on table 'Papers' 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.
ChartOfAccount 类:
public partial class ChartOfAccount
{
public ChartOfAccount()
{
this.PapersSale = new HashSet<Paper>();
this.PapersCostOfSale = new HashSet<Paper>();
this.PapersInventory = new HashSet<Paper>();
}
[StringLength(50)]
[ScaffoldColumn(true)]
public string Id { get; set; }
[StringLength(50)]
public string Name { get; set; }
public virtual ICollection<Paper> PapersSale { get; set; }
public virtual ICollection<Paper> PapersCostOfSale { get; set; }
public virtual ICollection<Paper> PapersInventory { get; set; }
}
论文类:
public class Paper
{
[Key]
[ScaffoldColumn(false)]
public int Id { get; set; }
[DisplayName("Name")]
[Required(ErrorMessage = "Name is required")]
[StringLength(50)]
public string Name { get; set; }
[DisplayName("Chart Of Account For Sale")]
[Required(ErrorMessage = "Chart Of Account is required")]
[StringLength(50)]
public string ChartOfAccountIdSale { get; set; }
[DisplayName("Chart Of Account For Inventory")]
[Required(ErrorMessage = "Chart Of Account is required")]
[StringLength(50)]
public string ChartOfAccountIdInventory { get; set; }
[DisplayName("Chart Of Account For Cost Of Sale")]
[Required(ErrorMessage = "Chart Of Account is required")]
[StringLength(50)]
public string ChartOfAccountIdCostOfSale { get; set; }
[ForeignKey("ChartOfAccountIdSale")]
public virtual ChartOfAccount ChartOfAccountSale { get; set; }
[ForeignKey("ChartOfAccountIdInventory")]
public virtual ChartOfAccount ChartOfAccountInventory { get; set; }
[ForeignKey("ChartOfAccountIdCostOfSale")]
public virtual ChartOfAccount ChartOfAccountCostOfSale { get; set; }
}
【问题讨论】:
标签: c# asp.net-mvc ef-code-first foreign-key-relationship