如何在实体框架中设置OnDelete级联

时间:2014-05-19 12:08:28

标签: c# entity-framework orm cascading-deletes entity-framework-6.1

我有两个表,BookCategory&图书。一个类别可以有多本书。 我正在使用实体框架。 在删除特定的BookCategory时,我想删除所有相同类别的书籍。

我不确定在哪里设置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] My Book Class(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>

enter image description here

但在删除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();

2 个答案:

答案 0 :(得分:0)

我认为你可以这样做(recordToDelete是你的BookCategory实例)

  recordToDelete.Books.ToList().ForEach(p => db.Books.Remove(p));


  db.BookCategory(recordToDelete).State = EntityState.Deleted;


  db.SaveChanges();

答案 1 :(得分:0)

我得到了解决方案,实际上它不是解决方案,因为没有设置数据库OnDelete Cascade是错误的。一旦配置,EF的Ondelete Cascade并不重要。