BookCategory & 두 개의 테이블이 있습니다. 하나의 카테고리에 대해 여러 권의 책이있을 수 있습니다. 엔티티 프레임 워크를 사용하고 있습니다. 특정 BookCategory를 삭제할 때 동일한 카테고리의 모든 책을 삭제하려고합니다.엔티티 프레임 워크에서 OnDelete 캐스케이드를 설정하는 방법
OnDelete = Cascade 규칙을 어디에 설정해야할지 모르겠습니다. 다음은 제 코드입니다.
추가 정보가 필요하면 알려주십시오.
1] 내 데이터베이스 (bookCatalog.Context.cs)
namespace LearningEF
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class bookCatalogEntities : DbContext
{
public bookCatalogEntities()
: base("name=bookCatalogEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<BookCategory> BookCategories { get; set; }
public virtual DbSet<Book> Books { get; set; }
}
}
2] 내 예약 클래스 (Book.cs)
namespace LearningEF
{
using System;
using System.Collections.Generic;
public partial class Book
{
public Book()
{
}
public int Id { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public Nullable<int> Category { get; set; }
public virtual BookCategory BookCategory { get; set; }
}
}
3] 내 BookCategories 클래스 (BookCategory.cs)
namespace LearningEF
{
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class BookCategory
{
public BookCategory()
{
this.Books = new HashSet<Book>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
}
4 다이어그램 XML (bookCatalog.edmx.diagram)
,<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram DiagramId="3da0fdfb8bce456da4d1833a61ed8d58" Name="Diagram1">
<EntityTypeShape EntityType="bookCatalogModel.Author" Width="1.5" PointX="0.75" PointY="1.25" IsExpanded="true" />
<EntityTypeShape EntityType="bookCatalogModel.BookCategory" Width="1.5" PointX="0.75" PointY="5.25" IsExpanded="true" />
<EntityTypeShape EntityType="bookCatalogModel.Book" Width="1.5" PointX="3" PointY="0.875" IsExpanded="true" />
<EntityTypeShape EntityType="bookCatalogModel.sysdiagram" Width="1.5" PointX="2.75" PointY="4.75" IsExpanded="true" />
<AssociationConnector Association="bookCatalogModel.FK_Books_BookCategories" ManuallyRouted="false" />
<AssociationConnector Association="bookCatalogModel.BookAuthors" ManuallyRouted="false" />
</Diagram>
</edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
하지만 삭제 BookCategory는 다음과 같은 오류를 가져 오는 동안. 여기
An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code
Additional information: An error occurred while updating the entries. See the inner exception for details.
카테고리를 삭제하는 내 코드입니다
BookCategory deleteBookCat = dbCatlog.BookCategories.SingleOrDefault(p => p.Id.Equals(1));
dbCatlog.BookCategories.Remove(deleteBookCat);
dbCatlog.SaveChanges();
모델 내부에서 설정해야합니다. 디자이너와 함께 edmx를 열고 속성 창에서 엔터티를 연결하는 선을 선택하면 변경할 OnDelete 속성이 있습니다. –
예, 저도 해 보았습니다. 스크린 샷을 첨부하려고합니다.하지만 여전히 오류가 발생했습니다. – AK47
EF는 현재 삭제 된 컨텍스트 내에로드 된 것들만 삭제할 것이므로 전체 데이터베이스를 지우지는 않을 것이므로 모든 것이로드되었는지 확인해야합니다. –