简体   繁体   English

asp.NET LINQ从数据库中删除

[英]asp.NET LINQ Delete from database

I have a SQL Server database with 2 tables: 我有一个带有2个表的SQL Server数据库:

t1 - Category t1 - 类别

Id
Name

t2- Product t2-产品

Id
Name
CategoryId

I want to delete a row from the Category table, but since I have the foreign key I need to handle the products that has the CategoryId I want to delete. 我想从Category表中删除一行,但由于我有外键,我需要处理具有我要删除的CategoryId的产品。

So I did this: 所以我这样做了:

var ProdCatID = (from prod in DataContext.Products
                 where prod.CategoryId == Convert.ToInt32(Id)
                 select prod).First();

ProdCatID.CategoryId = null;
DataContext.SubmitChanges();

var DelCat = (from cat in DataContext.Categories
             where cat.Id == Convert.ToInt32(Id)
             select cat).ToList();

DataContext.Categories.DeleteAllOnSubmit(DelCat);
DataContext.SubmitChanges();

What I m trying to do is to check if there is any product with that CategoryId , if there is - I want to set the CategoryID to null and then delete the row from the Category` table. m trying to do is to check if there is any product with that CategoryId的m trying to do is to check if there is any product with that , if there is - I want to set the CategoryID , if there is - I want to set the to null and then delete the row from the Category`表中to null and then delete the row from the

It is working when I have a product with a CategoryId but when I can't delete it. 当我有一个带有CategoryId的产品但是当我无法删除它时,它正在工作。

Any ideas? 有任何想法吗?

You're only setting the first product that has this CategoryID to null - you need to handle all products that have that ID ! 您只是将具有此CategoryID的第一个产品设置为null - 您需要处理具有该ID的所有产品!

var products = (from prod in DataContext.Products
                where prod.CategoryId == Convert.ToInt32(Id)
                select prod).ToList();

foreach(Product p in products)
{
    p.CategoryId = null;
}

DataContext.SubmitChanges();

.....

After that, now you should be able to delete the category from the table 之后,现在您应该能够从表中删除该类别

Simple! 简单! Change the Product table configuration in Database! 更改数据库中的Product表配置!

ALTER TABLE Product 
ADD CONSTRAINT 'Category_FK'
    FOREIGN KEY (CategoryId)
    REFERENCES Category(Id)
    ON DELETE SET NULL;

whenever you delete a primary key will automatically put null! 无论何时删除主键都会自动放入null!

Cascade on Delete is there in entity framework. Cascade on Delete存在于实体框架中。 Cascade delete automatically deletes dependent records or set null to foreignkey properties when the principal record is deleted. 删除主记录时,级联删除会自动删除相关记录或将null设置为foreignkey属性。

This is one to many reletionship between parent and child 这是父母与子女之间的一对多关系

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{   
    modelBuilder.Entity<User>()
        .HasOptional(a => a.UserDetail)
        .WithOptionalDependent()
        .WillCascadeOnDelete(true);
}

For more details check this: http://www.entityframeworktutorial.net/code-first/cascade-delete-in-code-first.aspx 有关详细信息,请查看: http//www.entityframeworktutorial.net/code-first/cascade-delete-in-code-first.aspx

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

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