먼저 ef 코드를 사용하여 동일한 테이블에서 다 대다 관계를 수행 할 수 없습니다. 누구든지 나를 도울 수 있습니까? Category 클래스와 CategoryRelation 클래스도 있습니다. Categoy 클래스에는 많은 상위 범주와 많은 하위 범주가 있습니다. 하지만 CategoryRelation에서 DisplayId로 추가 소품이 있습니다. 내 수업은 다음과 같습니다.
범주:
public class Category : Base
{
#region Top Menu
public int DisplayOrder { get; set; }
public bool ShowOnTopMenu { get; set; }
#endregion
public bool ShowOnTopSelling { get; set; }
[MaxLength(100)]
public string Name { get; set; }
#region Types
public bool IsPieceType { get; set; }
public bool IsGildingType { get; set; }
public bool IsPaperType { get; set; }
public bool IsSizeType { get; set; }
public bool IsWeightType { get; set; }
public bool IsCellophaneType { get; set; }
public bool IsCuttingType { get; set; }
public bool IsPrintingType { get; set; }
#endregion
public virtual ICollection<CategoryRelation> ChildCategoryList { get; set; }
public virtual ICollection<CategoryRelation> ParentCategoryList { get; set; }
public virtual ICollection<Product> ProductList { get; set; }
public virtual ICollection<WishList> WishList { get; set; }
public virtual ICollection<Description> DescriptionList { get; set; }
public virtual ICollection<Comment> CommentList { get; set; }
public virtual ICollection<Image> ImageList { get; set; }
public virtual ICollection<Template> TemplateList { get; set; }
public virtual ICollection<PromotionCode> PromotionCodeList { get; set; }
}
범주 관계;
public class CategoryReletion : Base
{
#region Parent Category
[ForeignKey("ParentCategory")]
public int ParentId { get; set; }
public Category ParentCategory { get; set; }
#endregion
#region Child Category
[ForeignKey("ChildCategory")]
public int ChildId { get; set; }
public Category ChildCategory { get; set; }
#endregion
public int DisplayOrder { get; set; }
}
마지막으로이 오류가 발생합니다.
'CategoryReletion'테이블에 FOREIGN KEY 제약 조건 'FK_dbo.CategoryReletion_dbo.Category_ParentId'를 도입하면주기 또는 다중 캐스케이드 경로가 발생할 수 있습니다. ON DELETE NO ACTION 또는 ON UPDATE NO ACTION을 지정하거나 다른 FOREIGN KEY 제약 조건을 수정합니다. 제약 조건 또는 인덱스를 생성 할 수 없습니다. 이전 오류를 참조하십시오. *
당신은 당신이 말한대로 정확하게 할 수 있습니다 (당신의 컨텍스트는 name이라고 가정합니다 MyContext
)
public class MyContext: DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<CategoryReletion> CategoryReletions { get; set; }
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<CategoryReletion>()
.HasRequired( c => c.ParentId)
.WithRequiredDependent()
.WillCascadeOnDelete(false);
modelBuilder.Entity<CategoryReletion>()
.HasRequired( c => c.ChildId)
.WithRequiredDependent()
.WillCascadeOnDelete(false);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다