나는 TypeListItem
의 목록을 포함하는 세 개의 속성 (Id, TypeListId, Name)
이있는 (Id, TypeListId, Name)
이라는 모델을 가지고 있습니다.
예 :
ProductType : 강력 함, 약함, 보통
ProductCategory : 카테고리 1, 카테고리 2, 카테고리 3
다음과 같은 Product 모델이 있습니다.
public int Id {get; set;}
public string Name {get; set;}
public int ProductTypeId {get; set;}
public int ProductCategoryId {get; set;}
// Navigation Properties
public TypeListItem ProductType {get; set;}
public TypeListItem ProductCategory {get; set;}
그러나, 내가 마이 그 레이션을 추가하고 다음 dotnet ef database update
'오류가 발생 :
FOREIGN KEY 제약 조건 소개 : 'Product'테이블의 FK_Product_TypeListItem_ProductCategoryId가 순환 또는 여러 개의 계단식 경로를 유발할 수 있습니다.
나 또한 유창한 API를 사용하여 다음과 같은 :
builder.Entity<Product>()
.Property(p => p.ProductTypeId)
.IsRequired();
builder.Entity<Product>()
.Property(p => p.ProductCategoryId)
.IsRequired();
같은 개체를 사용하는 두 탐색 속성이 있고 둘 다 필요하기 때문에 오류가 있다고 생각합니다.
어떤 추천? 내 모델에 문제가 있습니까?
이런 것이 ef core에 의미가 있습니까?
builder.Entity<Product>()
.HasOne(p => p.ProductType)
.WithOne().OnDelete(DeleteBehavior.Restrict);
builder.Entity<Product>()
.HasOne(p => p.ProductCategory)
.WithOne().OnDelete(DeleteBehavior.Restrict);
아래와 같이 OnDelete(DeleteBehavior.Restrict)
를 사용해야합니다.
노트 :
제한 : 삭제 작업은 종속 엔터티에 적용되지 않습니다. 종속 엔티티는 변경되지 않습니다.
builder.Entity<Product>()
.Property(p => p.ProductTypeId)
.WithOne()
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Product>()
.Property(p => p.ProductCategoryId)
.WithOne()
.OnDelete(DeleteBehavior.Restrict);
자세한 내용은 다음을 참조하십시오 : 계단식 삭제