简体   繁体   English

如何删除没有子类别的类别

[英]How to remove category without sub category

How to remove category without sub category ?如何删除没有子类别的类别?

Category Model :类别型号:

   public class Category 
    {
        public virtual int Id{ get; set; }
        public virtual string Name { get; set; }
        public virtual Category Parent { get; set; }
        public virtual int? ParentId { get; set; }
    }

datas :数据:

Id          ParentId    Name
1           null        Hot
2           1           Soup
3           1           Coffee
4           3           Decaf Coffee
5           null        Cold
6           5           Iced Tea

i need to remove Category with Id=1 but The following error occurs :我需要删除Id=1类别,但发生以下错误:

The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "FK_dbo.Categories_dbo.Categories_ParentId". DELETE 语句与 SAME TABLE REFERENCE 约束“FK_dbo.Categories_dbo.Categories_ParentId”相冲突。 The conflict occurred in database "ProjectDatabase", table "dbo.Categories", column 'ParentId'.冲突发生在数据库“ProjectDatabase”、表“dbo.Categories”、“ParentId”列中。 The statement has been terminated.该语句已终止。

my delete code :我的删除代码:

  public void Delete(int categoryId)
        {
            var category = _categories.First(d => d.Id == categoryId);
            _categories.Remove(category);
        }

CategoryConfig :类别配置:

 public class CategoryConfig : EntityTypeConfiguration<Category>
    {
        public CategoryConfig()
        {
            ToTable("Categories");
            HasOptional(x => x.Parent)
            .WithMany()
            .HasForeignKey(x => x.ParentId)
            .WillCascadeOnDelete(false);

       }
    }

You can't delete the category with Id=1 because there are subcategories referenced to it ( Soup and Coffee ).您不能删除Id=1的类别,因为有引用它的子类别( SoupCoffee )。 In your delete method you'll have to either change the ParentId of these items first to another existing element (or null ) or delete these items before deleting the category with Id=1 .在您的删除方法中,您必须先将这些项目的ParentId更改为另一个现有元素(或null ),或者在删除Id=1的类别之前删除这些项目。

Well, according to the documentation , if a foreign key on the dependent entity is nullable , Code First does not set cascade delete on the relationship (which you are doing explicitly), and when the principal is deleted the foreign key will be set to null .好吧,根据文档如果依赖实体上的外键可以为nullable ,则 Code First 不会在关系上设置级联删除(您正在明确这样做),并且当主体被删除时,外键将设置为null .

I don't know why is not doing that in your case, probably is because you are working with an unidirectional relationship and you don't have the collection of children in your parent Category, so EF couldn't set the FK properties to null in the subcategories, but you can try the following:我不知道为什么在您的情况下不这样做,可能是因为您使用的是单向关系,并且您的父类别中没有孩子的集合,因此 EF 无法将 FK 属性设置为null在子类别中,但您可以尝试以下操作:

var category = _categories.First(d => d.Id == categoryId);
var children=_categories.Where(d=>d.ParentId==categoryId);
foreach(var c in children)
   c.ParentId=null;
_categories.Remove(category);

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

相关问题 选择一个带有子类别的类别并将它们放入一个没有类别重复的 DataGridView - SELECT a category with it sub categories and put them into a DataGridView without category duplicates 如何获取类别或子类别中的所有产品 - How to get all Products within a Category or sub-category 在后面的代码中的类别下添加子类别 - Adding Sub category under category in code behind linq to SQL类别子类别计数递归 - linq to SQL category sub category count Recursive 单个Dropdownlistfor中的类别和子类别-MVC剃刀 - Category and Sub Category in single Dropdownlistfor - MVC Razor 如何转换IQueryable <Category> 到类别 - how to convert IQueryable<Category> to Category 如何匹配没有特定类别的方法 - How to match methods without a specific category 如何在不带数据库的情况下每次使用带有类别的子类别中的搜索? - How can i use Searching in sub category with category with out going to database each time? 如何使用C#在属性网格中实现子类别元素 - How to implement sub category elements in Property Grid using C# 使用Entity Framework和LINQ从数据库查询动态类别/子类别 - Querying Dynamic Category /Sub Category from database with Entity Framework and LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM