简体   繁体   English

将子实体移动到新的父实体

[英]Move child entities to a new parent entity

What is the best way to move child entities from one parent entity to another?将子实体从一个父实体移动到另一个父实体的最佳方法是什么? Is there a method inside the ObjectContext or DbContext that allows us to accomplish this? ObjectContext 或 DbContext 中是否有允许我们完成此任务的方法?

public class Person
{
   public int PersonId
   public ICollection<Car> Cars
}

public class Car
{
   public int CarId
   public string Color
}

EDIT: I'm currently using EF 4.0 model first with POCO template.编辑:我目前首先使用 EF 4.0 model 和 POCO 模板。

I'd say what you want to accomplish is changing the owner of the car in this example.我想说你想要完成的是在这个例子中改变车主 If there are no serious cons against adding a back reference to Person in the Car i'd go with something like:如果没有严重的缺点反对添加对Car中的Person的反向引用,我会使用类似以下内容的 go:

public class Car
{
   ...
   public virtual Person Owner { get; protected set; }

   public void ChangeOwner(Person newOwner)
   {
          // perform validation and then 
          Owner = newOwner;
          // maybe perform some further domain-specific logic
   }
}

NOTE: the protected setter is to enforce calling the ChangeOwner method by external consumers.注意:受保护的设置器将强制外部消费者调用ChangeOwner方法。 EF wil be able to set it properly thanks to the autogenerated proxies for POCO classes (assume you use them).由于 POCO 类的自动生成代理(假设您使用它们),EF 将能够正确设置它。

EDIT: In case there is no possibility to add a back reference to Person , you still have have the same goal looking from the domain logic perspective.编辑:如果不可能添加对Person的反向引用,从域逻辑的角度来看,您仍然有相同的目标。 You just want to change owner of a car.你只是想改变一辆车的主人。 Such operation involves two entites so i'd probably go with a method placed somewhere outside the entity (regardless of where it should be placed in a well designed system):这样的操作涉及两个实体,所以我可能会 go 使用放置在实体外部某处的方法(无论它应该放置在设计良好的系统中的哪个位置):

public void ChangeCarOwner(Person originalOwner, Person newOwner, int carId)
{
    Car car = originalOwner.RemoveCarOwnership(carId);
    newOwner.AddCarOwnership(car);
}

public class Person
{
    ...
    public Car RemoveCarOwnership(int carId)
    {
        Car car = this.Cars.Single(c => c.Id == carId);
        this.Cars.Remove(car);
        return car;
    }
}

This is just a conceptual piece of code and it most certainly can be written better (making sure the old owner actually owns the car etc.), but i just wanted to present an idea of how would i approach it.这只是一段概念性的代码,它肯定可以写得更好(确保老车主确实拥有这辆车等),但我只是想提出一个我将如何处理它的想法。 I also ommited the implementation of AddCarOwnership cause i suppose it's pretty strainghtforward.我还省略了AddCarOwnership的实现,因为我认为它非常紧张。 I introduced those methods cause adding and removing ownership may trigger some further logic "inside" a particular person.我介绍了这些方法,因为添加和删除所有权可能会触发特定人“内部”的一些进一步的逻辑。

With modern EFCore, you can do this very simply by Attaching the new Parent entity, which contains Children with IDs in them.使用现代 EFCore,您可以非常简单地通过附加新的父实体来完成此操作,其中包含具有 ID 的子实体。 It will reassign the FK of the child (or create it if no ID is specified), and create the new Person, all in one go它将重新分配孩子的FK(如果没有指定ID,则创建它),并创建新的Person,全部在一个go

Ex:前任:

var newOwner = new Person { 
    Cars = new List<Car> { 
        new Car { carId = theCarToMove.carId } 
    } 
};
Context.Attach(newOwner);
await Context.SaveChangesAsync();

Beware that Attach can cause problems if your Context isn't truly transient, but as a bandaid you could always clear the ChangeTracker before attempting an Attach请注意,如果您的 Context 不是真正的瞬态,则 Attach 可能会导致问题,但作为创可贴,您始终可以在尝试 Attach 之前清除 ChangeTracker

Sorry for necro but this simple solution wasn't anywhere that I could see对不起,我看不到这个简单的解决方案

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

相关问题 我可以配置Fluent NHibernate将已删除的父级的所有子级实体移动到新的父级吗? - Can I configure Fluent NHibernate to move all child entities of deleted parent to new parent? 实体框架 - 未在保存父实体上添加到数据库的新子实体(仅在生产环境中) - Entity Framework - New child entities not added to database on saving parent entity (only in production environment) 子实体添加到父实体时不保留 - Child entities not persisting when added to parent entity 从父实体更新子实体 - Update child entities from parent entity 多个父实体首先在EF代码中包含一个子实体 - multiple parent Entities with one child entity in EF code first NHibernate - Cascade Merge到子实体对于分离的父实体失败 - NHibernate - Cascade Merge to child entities fails for detached parent entity 将父表/实体与子表/实体连接的好方法? - Good way to connect parent table/entity with child table/entities? SQLite / C#:设计一个实体,该实体是许多父实体的子实体 - SQLite/C#: Design a entity which is a child of many parent entities 无法在EF4中将父实体与现有子实体一起添加 - Unable to add parent entity with existing child entities in EF4 在 EF 中更新父实体时更新子实体 - update child entities when updating a parent entity in EF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM