簡體   English   中英

使用通用存儲庫更新多對多關系

[英]Updating many-to-many relationships with a generic 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...
}

這是用戶模型。

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; }
}

如果我修改了業務模型集合,雖然我已經附加了整個實體,但它並沒有保存業務模型集合。

Worker.UserRepository.Update(user);

我不確定發生了什么。 我不想破壞我的通用存儲庫/工作單元模式只是為了更新多對多關系。

編輯2:我有這個工作......但它與我想要的模式有很大的不同。 有了硬實現意味着我需要為每個具有多對多關系的類型創建一個方法。 我現在正在調查,看看我是否可以將其作為通用方法。

編輯3:所以之前的實現我沒有像我想的那樣工作。 但現在,我有一個稍微有效的實現。 如果有人願意幫助我,我可以繼續這樣做,我會永遠愛你。

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;
}

然后我這樣稱呼它:

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

非常混亂,但它讓我可以更新與泛型的多對多關系。 當我去附加已經存在的新值時,我的大問題是異常。 由於include語句,它們已經加載。

這有效:

更新自更新到

這不是:

更新自更新到

經過許多痛苦的工作,我終於找到了一種方法來更新與完全通用的存儲庫的多對多關系。 這將允許我創建(並保存)許多不同類型的實體,而無需為每個實體創建樣板代碼。

此方法假定:

  • 您的實體已存在
  • 您的多對多關系存儲在具有復合鍵的表中
  • 您正在使用預先加載來將關系加載到上下文中
  • 您正在使用工作單元/通用存儲庫模式來保存實體。

這是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;
}

這是我在網上找到的一個幫助方法,它允許我根據我提供的任何類型創建通用列表。

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

從現在開始,這就是要求更新多對多關系的內容:

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

當然,最后你需要去某個地方打電話:

Context.SaveChanges();

我希望這可以幫助那些從未真正發現如何在Entity Framework中使用通用存儲庫和工作單元類的多對多關系的人。

@dimgl您的解決方案對我有用。 我另外做的是用動態檢索的主鍵替換主鍵的硬編碼類型和名稱:

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);
            }

像這樣,您的代碼將不依賴於實體密鑰的名稱和類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM