簡體   English   中英

如何檢查集合的兩個實體條目是否相同

[英]How to check if two entity entries of a collection are the same

我試圖做一個通用的方法來更新實體框架集合,一對多。 我做了這種方法,但是當我嘗試驗證新集合中的元素是否已經存在於舊集合中時,我遇到了問題。 如果存在,我必須對其進行更新,而不是刪除並再次添加。

代碼是這樣的:

public TEntity UpdateCollection<TEntity, TChildren>(myappContext dbContext, TEntity parentObject, Expression<Func<TEntity, 
            ICollection<TChildren>>> propertyExpression, ICollection<TChildren> objectChilren) where TEntity : class where TChildren : class
        {

            var parentEntityObject = dbContext.Entry<TEntity>(parentObject);
            List<TChildren> originalChildrenData = parentEntityObject.Collection(propertyExpression).CurrentValue.ToList();

            // Updating or removing existing items
            foreach (var originalItem in originalChildrenData)
            {
                // Where the problem is: If entry was just modified, i have to update.
                var newItem = objectChilren.FirstOrDefault(x => x == originalItem);
                if (newItem != null)
                {
                    dbContext.Entry<TChildren>(originalItem).CurrentValues.SetValues(newItem);
                    dbContext.Entry<TChildren>(originalItem).State = System.Data.EntityState.Modified;
                }
                else
                {
                    dbContext.Entry<TChildren>(originalItem).State = System.Data.EntityState.Deleted;
                }
            }

            // Adding new items
            foreach(var newItem in objectChilren.Except(originalChildrenData)){
                parentEntityObject.Collection(propertyExpression).CurrentValue.Add(newItem);
            }

            parentEntityObject.State = System.Data.EntityState.Modified;
            return parentEntityObject.Entity;
        }

而不是嘗試檢查:

var newItem = objectChilren.FirstOrDefault(x => x == originalItem);
                    if (newItem != null)

我也嘗試過:

var newItem = this.Set<TChildren>().Local.FirstOrDefault(x => x == originalItem);

但也不起作用,總是返回null。 我必須獲取相應的條目並僅對其進行更新。

如果不可能,還有另一種通用的方法可以“一對多”更新集合嗎?

我做到了,就像Magnus所建議的那樣,比較了我收藏中的關鍵屬性,如下所示:

public TEntity UpdateCollection<TEntity, TChildren>(myappContext dbContext, TEntity parentObject, Expression<Func<TEntity, 
            ICollection<TChildren>>> propertyExpression, ICollection<TChildren> objectChilren) where TEntity : class where TChildren : class
        {

            var parentEntityObject = dbContext.Entry<TEntity>(parentObject);
            List<TChildren> originalChildrenData = parentEntityObject.Collection(propertyExpression).CurrentValue.ToList();

            // Get key name
            var entityKeyName = GetKeyName(dbContext, originalChildrenData.Union(objectChilren).First());

            // Updating or removing existing items
            foreach (var originalItem in originalChildrenData)
            {

                var originalValueKey = originalItem.GetType().GetProperty(entityKeyName).GetValue(originalItem, null);
                var itemCompareExpression = GetCompareExpression<TChildren>(entityKeyName, originalValueKey);

                // If entry was just modified, i have to update.
                var newItem = objectChilren.FirstOrDefault(itemCompareExpression.Compile());
                if (newItem != null)
                {
                    dbContext.Entry<TChildren>(originalItem).CurrentValues.SetValues(newItem);
                    dbContext.Entry<TChildren>(originalItem).State = System.Data.EntityState.Modified;
                    // Remove item, because only 'new items' will be added after this loop
                    objectChilren.Remove(newItem);
                }
                else
                {
                    dbContext.Entry<TChildren>(originalItem).State = System.Data.EntityState.Deleted;
                }
            }

            // Adding new items
            foreach(var newItem in objectChilren)
            {
                parentEntityObject.Collection(propertyExpression).CurrentValue.Add(newItem);
            }

            parentEntityObject.State = System.Data.EntityState.Modified;
            return parentEntityObject.Entity;
        }

方法稱為:

public string GetKeyName<TEntity>(myappContext dbContext, TEntity entity) where TEntity : class
        {
            ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
            ObjectSet<TEntity> set = objectContext.CreateObjectSet<TEntity>();
            return set.EntitySet.ElementType.KeyMembers.FirstOrDefault().Name;
        }

        public Expression<Func<TEntity, bool>> GetCompareExpression<TEntity>(string keyName, object value) where TEntity : class
        {
            var parameter = Expression.Parameter(typeof(TEntity), "x");
            var property = Expression.Property(parameter, keyName);
            var method = property.Type.GetMethod("Equals", new[] { property.Type });
            var convertedValue = Convert.ChangeType(value, property.Type);
            var expression = Expression.Call(property, method, Expression.Constant(convertedValue));

            return Expression.Lambda<Func<TEntity, bool>>(expression, parameter);
        }

由於我所有的實體只有一個鍵,因此我只在“ GetKeyName”上使用了“ First”,但如有必要,它將返回所有鍵。

暫無
暫無

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

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