[英]How to recursively check if the entity has a collection of children of that same entity?
[英]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.