简体   繁体   English

如何在EF4中将新的子实体添加到父实体?

[英]How do you add new child entities to a parent in EF4?

I am working with VS2010, .NET 4.0, Entity Framework 4, and POCO entities. 我正在使用VS2010,.NET 4.0,实体框架4和POCO实体。 I am trying to add and remove child entities from a parent. 我正在尝试从父级添加和删除子级实体。 I have a many-to-many relationship between [User] and [Companies]. 我在[用户]和[公司]之间有多对多关系。 So I have the following entity: 所以我有以下实体:

// Objects created by POCO template generator
public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public ICollection<Company> Companies { get; set; }
}
public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<User> Users { get; set; }
}

At this point in the code, the User exists but has ZERO companies. 在代码的这一点上,用户存在,但拥有零公司。 What I am trying to do is add a new child Company to the User. 我想做的是向用户添加一个新的子公司。 This code DOES save the Company but it is also creating a duplicate User parent. 此代码确实保存了公司,但同时也创建了重复的User父级。

public UserRepository
{
    internal ObjectContextEntities _dbContext = new ObjectContextEntities();
    public User Save(User pocoObject)
    {
        // Load the existing User which has no companies
        var loadedEntity = _dbContext.Users.Where(m => m.Id == pocoObject.Id).FirstOrDefault();

        // Add the new company from a POCO object
        loadedEntity.Companies.Add(pocoObject.Companies.Last());

        _dbContext.SaveChanges();
    }
}

How should I be adding child entities? 我应该如何添加子实体? (Without creating a duplicate relationship?) ... Also is there a simple way to update those children when performing "ApplyCurrentValues()" on the parent User? (不创建重复关系吗?)...还有在父用户上执行“ ApplyCurrentValues()”时更新那些子级的简单方法吗?

This code does work and does not create duplicate users. 此代码可以正常工作,并且不会创建重复的用户。

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public ICollection<Company> Companies { get; set; }

    public User()
    {
        Companies = new List<Company>();
    }
}

public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class POCOEntitiesContext : ObjectContext
{
    private ObjectSet<User> users;
    public ObjectSet<User> Users
    {
        get { return users; }
    }

    private ObjectSet<Company> companies;
    public ObjectSet<Company> Companies
    {
        get { return companies; }
    }

    public POCOEntitiesContext() : base("name=POCOEntities", "POCOEntitiesContainer")
    {
        users = CreateObjectSet<User>();
        companies = CreateObjectSet<Company>();
    }
}

using (var ctx = new POCOEntitiesContext())
{
    var loadedUser = ctx.Users.Where(u => u.Username == "Peri").First();
    loadedUser.Companies.Add(new Company() { Name = "New Company" });
    ctx.SaveChanges();
}

I tried a more simplified approach and I figured it out: 我尝试了一种更简化的方法,并弄清楚了:

    var newCompany = pocoObject.Companies.Last();
    newCompany.Users = null;
    loadedUser.Companies.Add(newCompany);

It turns out because there was a reference to the parent INSIDE the child, it was propagating up and causing a new parent to be created. 原来是因为在孩子内部有对父对象的引用,它正在传播并导致创建新的父对象。 So from a POCO standpoint, I had to remove the parent and keep it a very simple object. 因此,从POCO的角度来看,我必须删除父对象并将其保留为非常简单的对象。 (Or I could have just removed the Child's Parent reference in the edmx). (或者我可能刚刚在edmx中删除了“孩子的父母”引用)。 But even this makes my life easier: 但这甚至使我的生活更轻松:

    loadedUser.Companies.Add(new Company() { Name = "New Company" });

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

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