对于数据库优先方法(.edmx),如何基于父级删除自动删除子级? [英] How to delete child automatically based on parent deletion for database first approach (.edmx)?

查看:85
本文介绍了对于数据库优先方法(.edmx),如何基于父级删除自动删除子级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的2类共享1对多的关系:

Below are my 2 class sharing 1 to many relationship :

public partial class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Skills> Skills { get; set; }
}

public partial class Skills
{
    public int Id { get; set; }
    public Nullable<int> EmployeeId { get; set; }
    public string Skills { get; set; }
    public virtual Employee Employee { get; set; }
}

现在,我尝试通过以下方式撤消具有相应技能的员工:

Now I am trying to remove employees with its corresponding skills in following way :

1)仅保存更改,仅用一种方法删除员工和技能。我猜在这种情况下,我将获得性能上的好处,因为我只需要调用一次保存更改,但是还有一个问题是,如果技能被删除,但是在删除员工的过程中发生错误,我会

1) Deleting both employee and skills in 1 method with only save changes. I guess I will be having performance benefit in this case as I need to call save changes only once but there is also 1 issue that if skills got deleted but if error occurs while deleting employee in that case I will lose Skills of corresponding Employee.

public void Delete(int[] ids)
{
    using (var context = new MyEntities())
    {
        context.Skills.RemoveRange(context.Skills.Where(cd => ids.Contains(cd.EmployeeId)));
        context.Employee.RemoveRange(context.Employee.Where(t => ids.Contains(t.Id)));
        context.SaveChanges();
    }
}

2)另一种选择是使用交易来确保和孩子都成功删除,如下所示:

2) Another option is to use transaction to make sure that both and child gets deleted successfully like below :

public HttpResponseMessage Delete(int[] ids)
{ 
    using (var context = new MyEntities())
    {
        using (var transaction = context.Database.BeginTransaction())
        {
            try
            { 
                DeleteSkills(ids,context);
                DeleteEmployees(ids,context);
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                // throw exception.
            }
        }
    }
}

public void DeleteEmployees(int[] ids,MyEntities _context)
{
    _context.Employee.RemoveRange(_context.Employee.Where(t => ids.Contains(t.Id)));
    _context.SaveChanges();
}

public void DeleteSkills(int[] ids, MyEntities _context)
{
    _context.Skills.RemoveRange(_context.Skills.Where(cd => ids.Contains(cd.EmployeeId)));
    _context.SaveChanges();
}

3)我正在寻找不需要删除的选项显式地删除子级(技能),并且根据父级(雇员)实体的删除情况自动删除子级,就像在删除代码时先执行级联的情况下发生的情况一样,这样我就不必触发2个查询来删除父级和子级(我第一种选择),或者我不必维持交易(第二种选择。)

3) I am looking for an option where I don't need to remove child (Skills) explicitly and child gets removed automatically based on removal of parent (Employee) entity like the way it happens in case of Code first Cascade on delete so that I don't have to fire 2 queries to remove parent and child (my first option) or I don't have to maintain transaction (my second option.)

我进行了一些研究,但找不到基于以下条件自动删除孩子的任何帮助:如果是数据库优先方法(.edmx),则删除父级。

I did some research but couldn't find any help on removing child automatically based on removal of parent in case of Database first approach (.edmx).

处理这种情况的有效方法是什么?

What is an efficient way to handle this scenario?

推荐答案

了解实体框架中的删除行为。

Read about delete behaviour in Entity Framework.

您可以选择实体在删除时的行为,以便

You can choose how an enitity behave on delete so it can affect child/dependant.

在您的情况下,您需要层叠删除行为,该行为会自动删除要删除的实体的子代/依赖者。

In your case you need "Cascade" delete behaviour which automatically delete children/dependants of the entity you are deleting.

在您的OnModelCreating方法中执行以下操作:

Do it like this in you OnModelCreating method:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PARENT>()
             .........
            .OnDelete(DeleteBehavior.Cascade);
    }

在这里了解如何使用以及它的用途:

Take a look here on how to use and what is about:

https://entityframeworkcore.com/saving-data-cascade-delete#:~:text=Entity%20Framework%20Core%20Cascade%20Delete& ; text = Cascade%20delete%20allows%20the%20deeletion,delete%20behaviors%20of%20individual%20relationships

这篇关于对于数据库优先方法(.edmx),如何基于父级删除自动删除子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆