简体   繁体   English

具有多对多关系的数据添加/删除

[英]Adding/Deleting Data with Many-to-Many Relationship

I'm trying to use Entity Framework 4.0 to add and remove records with a many-to-many relationship. 我正在尝试使用Entity Framework 4.0添加和删除具有多对多关系的记录。 I've scoured the web but am just not finding a meaningful example. 我曾在网上搜寻过,但没有找到有意义的例子。

As an example, let's say I have a Drivers and Vehicles table with a junction table VehicleDrivers. 举例来说,假设我有一个带有连接表VehicleDrivers的Drivers and Vehicles表。

Drivers 车手

  • DriverID 驱动ID
  • ... ...

Vehicles 车辆

  • VehicleID 车辆编号
  • ... ...

VehicleDrivers 车辆驾驶员

  • DriverID 驱动ID
  • VehicleID 车辆编号

My questions are: 我的问题是:

  1. How would I write EF statements to add a new vehicle and related driver? 我将如何编写EF语句以添加新的车辆和相关驾驶员?
  2. How would I write EF statements to delete an existing vehicle and any related drivers? 如何编写EF语句以删除现有车辆和任何相关驾驶员?

Thanks for any tips. 感谢您的提示。

Something like this: 像这样:

How would I write EF statements to add a new vehicle and related driver? 我将如何编写EF语句以添加新的车辆和相关驾驶员?

For existing driver: 对于现有驱动程序:

using (var context = new MyContext(...))
{
    var newVehicle = new Vehicle { ... };
    var existingDriver = context.Drivers.First(d => d.Name == "Jim");
    newVehicle.Drivers.Add(existingDriver);

    context.Vehicles.AddObject(newVehicle);

    context.SaveChanges();
}

For new driver: 对于新驱动程序:

using (var context = new MyContext(...))
{
    var newVehicle = new Vehicle { ... };
    var newDriver = new Driver { ... };
    newVehicle.Drivers.Add(newDriver);

    context.Vehicles.AddObject(newVehicle);

    context.SaveChanges();
}

How would I write EF statements to delete an existing vehicle and any related drivers? 如何编写EF语句以删除现有车辆和任何相关驾驶员?

using (var context = new MyContext(...))
{
    var existingVehicle = context.Vehicles.First(v => v.Name == "Ford");

    context.Vehicles.DeleteObject(existingVehicle);

    context.SaveChanges();
}

Nothing more to do here because the related entries in the join table with all drivers linked to the deleted vehicle will be deleted as well due to enabled cascading delete in the database. 此处无事可做,因为由于数据库中启用了级联删除,连接表中与所有链接到已删除车辆的驱动程序相关的条目也将被删除。

Edit 编辑

If you want to delete the drivers as well (and not only the links) in a many-to-many relationship you need to check first if the driver isn't linked to another vehicle: 如果您也要删除多对多关系中的驱动程序(而不仅仅是链接),则需要首先检查驱动程序是否未链接到另一辆车:

using (var context = new MyContext(...))
{
    var existingVehicle = context.Vehicles.Include("Drivers")
        .First(v => v.Name == "Ford");

    foreach(var driver in existingVehicle.Drivers.ToList())
    {
        if (!context.Drivers.Any(d => d.DriverId == driver.DriverId
            && d.Vehicles.Any(v => v.VehicleId != existingVehicle.VehicleId)))
            context.Drivers.DeleteObject(driver);
    }
    context.Vehicles.DeleteObject(existingVehicle);

    context.SaveChanges();
}

What I usually do is use the key field and place it into the object I'm saving. 我通常要做的是使用键字段并将其放入要保存的对象中。 This does not work well when you are modifying several new objects at the same time however. 但是,当您同时修改多个新对象时,这不能很好地工作。

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

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