简体   繁体   English

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

[英]How to set OnDelete cascade in entity framework

I have two tables, BookCategory & Books. 我有两个桌子,BookCategory和Books。 There can be multiple books for one category. 一个类别可以有多本书。 I am using entity framework. 我正在使用实体框架。 On deleting particular BookCategory, I want to delete all books of same category. 在删除特定的BookCategory时,我想删除同一类别的所有图书。

I am not sure where to set the rule of OnDelete = Cascade. 我不确定在哪里设置OnDelete = Cascade的规则。 Following is my code. 以下是我的代码。

Please let me know if any more information is required. 请让我知道是否需要更多信息。

1] My Database (bookCatalog.Context.cs) 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) 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] My BookCategories class (BookCategory.cs) 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] Diagram xml (bookCatalog.edmx.diagram) 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>

在此处输入图片说明

but while deleting BookCategory getting following error. 但是在删除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.

Here is my code of deleting category 这是我删除类别的代码

BookCategory deleteBookCat = dbCatlog.BookCategories.SingleOrDefault(p => p.Id.Equals(1));
            dbCatlog.BookCategories.Remove(deleteBookCat);
            dbCatlog.SaveChanges();

I think you can do something like this (recordToDelete is your instance of BookCategory) 我认为您可以做这样的事情(recordToDelete是BookCategory的实例)

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


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


  db.SaveChanges();

I got solution, Actually it is not solution, cause it was mistake that in Database OnDelete Cascade was not set. 我得到了解决方案,实际上这不是解决方案,因为在Database OnDelete Cascade中未设置错误。 Once it is configured, Ondelete Cascade of EF doesn't matter. 配置完成后,EF的Ondelete级联无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM