问题描述
我没有找到一个很好的解决方案,这个问题(类似的票在这里。
I found and followed this example: http://entityframeworktutorial.net/update-many-to-many-entities-in-entity-framework.aspx#.UTBeTDBhif8.
这样做很好,但它创建了相关实体的重复(使用sql profiler我看到Insert into RelatedEntities ..我只会在MyEntities_Related连接表中插入/更新,我不明白为什么EF在RelatedEntities表中插入:(
This works just fine, but it creates a duplicate of the related entities (with sql profiler I saw the Insert into RelatedEntities .. I would expected just an insert / update on the MyEntities_Related join table and I don't understand why EF makes an insert on the RelatedEntities table :(
这里是我的代码:
public void AddOrUpdate(MyEntity entity) {
var dbEntity = _context.MyEntities
.Include(e => e.RelatedEntitis)
.Where(e => e.ID == entity.ID)
.SingleOrDefault();
var newRelated = entity.RelatedEntitis.ToList<RelatedEntity>();
var dbRelated = dbEntity.RelatedEntity.ToList<RelatedEntity>();
_context.Entry(dbEntity).CurrentValues.SetValues(entity);
var comparer = new EqualityComparer();
var addedRelated = newRelated.Except(dbRelated, comparer).ToList<RelatedEntity>();
var deletedRelated = dbRelated.Except(newRelated, comparer).ToList<RelatedEntity>();
addedRelated.ForEach(e => dbEntity.RelatedEntity.Add(e));
deletedRelated.ForEach(e => dbEntity.RelatedEntity.Remove(e));
}
public class EqualityComparer : IEqualityComparer<RelatedEntity>
{
public int GetHashCode(RelatedEntity obj)
{
return (obj == null) ? 0 : obj.ID.GetHashCode();
}
public bool Equals(RelatedEntity x, RelatedEntity y)
{
if (ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
return x.ID == y.ID;
}
}
数据库模式如下所示:
|------------------|
| Table MyEntities |
|------------------|
| int ID //PK |
|------------------|
|--------------------------|
| Table MyEntities_Related |
|--------------------------|
| int MyEntityID |
| int RelatedID |
|--------------------------|
|-----------------------|
| Table RelatedEntities |
|-----------------------|
| int RelatedID //PK |
| string Name |
|-----------------------|
任何帮助不胜感激:)
推荐答案
解决方案
我不知道这是否是最好的,但它适用于我,我认为这是有道理的。 EF创建一个重复的相关性,因为它们是分离的,
我以这种方式改变了我的代码:
SOLUTIONI don't know if it's the best, but it works for me and I think it has sense. EF creates a duplicate of the relatedentities because they were detached,I changed my code in this way:
public void AddOrUpdate(MyEntity entity) {
var dbEntity = _context.MyEntities
.Include(e => e.RelatedEntitis)
.Where(e => e.ID == entity.ID)
.SingleOrDefault();
//replace entities with those taken from the context
var relatedEntities = _context.RelatedEntities;
var detachedEntities = entity.RelatedEntities;
entity.RelatedEntities = new List<RelatedEntity>();
foreach (var ent in detachedEntities)
{
entity.RelatedEntities.Add(relatedEntities .Where(e => e.ID == hop.ID).SingleOrDefault());
}
var newRelated = entity.RelatedEntitis.ToList<RelatedEntity>();
var dbRelated = dbEntity.RelatedEntity.ToList<RelatedEntity>();
_context.Entry(dbEntity).CurrentValues.SetValues(entity);
_context.Entry(dbEntity.RelatedEntity).CurrentValues.SetValues(entity.RelatedEntitis);
var comparer = new EqualityComparer();
var addedRelated = newRelated.Except(dbRelated, comparer).ToList<RelatedEntity>();
var deletedRelated = dbRelated.Except(newRelated, comparer).ToList<RelatedEntity>();
addedRelated.ForEach(e => dbEntity.RelatedEntity.Add(e));
deletedRelated.ForEach(e => dbEntity.RelatedEntity.Remove(e));
}
这篇关于EF5多对多更新断开连接的场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!