我有禁用延迟加载的数据库上下文。我正在使用急切加载来加载我的所有实体。我无法更新许多关系。
这是存储库。
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));
极为困惑,但它使我可以使用泛型更新多对多关系。我最大的问题是当我去附加已经存在的新值时出现异常。由于包含语句,它们已经被加载。
这有效:
这不是:
最佳答案
经过数小时的痛苦之后,我终于找到了一种使用完全通用的存储库更新多对多关系的方法。这将使我可以创建(并保存)许多不同类型的实体,而无需为每个实体创建样板代码。
此方法假定:
这是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 中使用通用存储库和工作单元类的多对多关系的人有所帮助。
关于c# - 使用通用存储库更新多对多关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21975419/