简体   繁体   English

使用通用存储库更新多对多关系

[英]Updating many-to-many relationships with a generic repository

I have a database context with lazy loading disabled. 我有一个禁用延迟加载的数据库上下文。 I am using eager loading to load all of my entities. 我正在使用急切加载来加载我的所有实体。 I cannot update many to many relationships. 我无法更新多对多的关系。

Here's the repository. 这是存储库。

public class GenericRepository<TEntity> : IGenericRepository<TEntity>
        where TEntity : class
{
    ... other code here...

    public virtual void Update(TEntity t)
    {
        Set.Attach(t);
        Context.Entry(t).State = EntityState.Modified;
    }

    ...other code here...
}

Here's the User model. 这是用户模型。

public partial class User
{
    public User()
    {
        this.Locks = new HashSet<Lock>();
        this.BusinessModels = new HashSet<BusinessModel>();
    }

    public int UserId { get; set; }
    public string Username { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
    public string JobTitle { get; set; }
    public string RecoveryEmail { get; set; }
    public Nullable<double> Zoom { get; set; }

    public virtual ICollection<Lock> Locks { get; set; }
    public virtual ICollection<BusinessModel> BusinessModels { get; set; }
}

If I modify the business models collection, it does not save the business models collection although I have attached the entire entity. 如果我修改了业务模型集合,虽然我已经附加了整个实体,但它并没有保存业务模型集合。

Worker.UserRepository.Update(user);

I'm not sure what is going on. 我不确定发生了什么。 I don't want to break my generic repository/unit of work pattern just to update many-to-many relationships. 我不想破坏我的通用存储库/工作单元模式只是为了更新多对多关系。

Edit 2: I've got this working...but it is extremely different from the pattern that I'm going for. 编辑2:我有这个工作......但它与我想要的模式有很大的不同。 Having hard implementations means I will need to create a method for each type that has a many to many relationship. 有了硬实现意味着我需要为每个具有多对多关系的类型创建一个方法。 I am investigating now to see if I can make this a generic method. 我现在正在调查,看看我是否可以将其作为通用方法。

Edit 3: So the previous implementation I had did not work like I thought it would. 编辑3:所以之前的实现我没有像我想的那样工作。 But now, I have a slightly working implementation. 但现在,我有一个稍微有效的实现。 If someone would please help me so I can move on from this, I will love you forever. 如果有人愿意帮助我,我可以继续这样做,我会永远爱你。

public virtual void Update(TEntity updated,
    IEnumerable<object> set,
    string navigationProperty,
    Expression<Func<TEntity, bool>> filter,
    Type propertyType)
{
    // Find the existing item
    var existing = Context.Set<TEntity>().Include(navigationProperty).FirstOrDefault(filter);

    // Iterate through every item in the many-to-many relationship
    foreach (var o in set)
    {
        // Attach it if its unattached
        if (Context.Entry(o).State == EntityState.Detached)
            // Exception "an object with the same key already exists"
            // This is due to the include statement up above. That statement
            // is necessary in order to edit the entity's navigation
            // property.
            Context.Set(propertyType).Attach(o);
    }

    // Set the new value on the navigation property.
    Context.Entry(existing).Collection(navigationProperty).CurrentValue = set;

    // Set new primitive property values.
    Context.Entry(existing).CurrentValues.SetValues(updated);
    Context.Entry(existing).State = EntityState.Modified;
}

I then call it like this: 然后我这样称呼它:

Worker.UserRepository.Update(user, user.BusinessModels, "BusinessModels", i => i.UserId == user.UserId, typeof (BusinessModel));

Extremely messy, but it lets me update many-to-many relationships with generics. 非常混乱,但它让我可以更新与泛型的多对多关系。 My big problem is the exception when I go to attach new values that already exist. 当我去附加已经存在的新值时,我的大问题是异常。 They're already loaded because of the include statement. 由于include语句,它们已经加载。

This works: 这有效:

更新自更新到

This doesn't: 这不是:

更新自更新到

After many painful hours, I have finally found a way to update many-to-many relationships with a completely generic repository. 经过许多痛苦的工作,我终于找到了一种方法来更新与完全通用的存储库的多对多关系。 This will allow me to create (and save) many different types of entities without creating boilerplate code for each one. 这将允许我创建(并保存)许多不同类型的实体,而无需为每个实体创建样板代码。

This method assumes that: 此方法假定:

  • Your entity already exists 您的实体已存在
  • Your many to many relationship is stored in a table with a composite key 您的多对多关系存储在具有复合键的表中
  • You are using eager loading to load your relationships into context 您正在使用预先加载来将关系加载到上下文中
  • You are using a unit-of-work/generic repository pattern to save your entities. 您正在使用工作单元/通用存储库模式来保存实体。

Here's the Update generic method. 这是Update通用方法。

public virtual void Update(Expression<Func<TEntity, bool>> filter,
    IEnumerable<object> updatedSet, // Updated many-to-many relationships
    IEnumerable<object> availableSet, // Lookup collection
    string propertyName) // The name of the navigation property
{
    // Get the generic type of the set
    var type = updatedSet.GetType().GetGenericArguments()[0];

    // Get the previous entity from the database based on repository type
    var previous = Context
        .Set<TEntity>()
        .Include(propertyName)
        .FirstOrDefault(filter);

    /* Create a container that will hold the values of
        * the generic many-to-many relationships we are updating.
        */
    var values = CreateList(type);

   /* For each object in the updated set find the existing
        * entity in the database. This is to avoid Entity Framework
        * from creating new objects or throwing an
        * error because the object is already attached.
        */
    foreach (var entry in updatedSet
        .Select(obj => (int)obj
            .GetType()
            .GetProperty("Id")
            .GetValue(obj, null))
        .Select(value => Context.Set(type).Find(value)))
    {
        values.Add(entry);
    }

    /* Get the collection where the previous many to many relationships
        * are stored and assign the new ones.
        */
    Context.Entry(previous).Collection(propertyName).CurrentValue = values;
}

Here's a helper method I found online which allows me to create generic lists based on whatever type I give it. 这是我在网上找到的一个帮助方法,它允许我根据我提供的任何类型创建通用列表。

public IList CreateList(Type type)
{
    var genericList = typeof(List<>).MakeGenericType(type);
    return (IList)Activator.CreateInstance(genericList);
}

And from now on, this is what calls to update many-to-many relationships look like: 从现在开始,这就是要求更新多对多关系的内容:

Worker.UserRepository.Update(u => u.UserId == user.UserId,
    user.BusinessModels, // Many-to-many relationship to update
    Worker.BusinessModelRepository.Get(), // Full set
    "BusinessModels"); // Property name

Of course, in the end you will need to somewhere call: 当然,最后你需要去某个地方打电话:

Context.SaveChanges();

I hope this helps anyone who never truly found how to use many-to-many relationships with generic repositories and unit-of-work classes in Entity Framework. 我希望这可以帮助那些从未真正发现如何在Entity Framework中使用通用存储库和工作单元类的多对多关系的人。

@dimgl Your solution worked for me. @dimgl您的解决方案对我有用。 What I've done in addition was to replace the hard-coded type and name of the primaryKey with dynamically retrieved ones: 我另外做的是用动态检索的主键替换主键的硬编码类型和名称:

ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext; 
ObjectSet<TEntity> set = objectContext.CreateObjectSet<TEntity>(); 
IEnumerable<string> keyNames = set.EntitySet.ElementType.KeyMembers.Select(k => k.Name); 

var keyName = keyNames.FirstOrDefault(); 
var keyType = typeof(TEntity).GetProperty(keyName).PropertyType

foreach (var entry in updatedSet
                .Select(obj =>
                    Convert.ChangeType(obj.GetType()
                                          .GetProperty(keyName)
                                          .GetValue(obj, null), keyType))
                .Select(value => context.Set<TEntity>().Find(value)))
            {
                values.Add(entry);
            }

Like this your code won't depend on the Entity key's name and type. 像这样,您的代码将不依赖于实体密钥的名称和类型。

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

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