我知道以下事实:已经有几个问题,但是没有一个能够真正解决我的问题:
应用背景
我有一个首先使用Entity Framework 6.1代码的Web应用程序。我在DAL中使用存储库模式,这意味着,我有Repo来查询我的上下文并将结果返回给Services,然后使用Automapper将我的实体映射到视图模型,然后返回VM。服务由Web API方法调用。
基于上述架构,很明显我正在使用分离的实体。
我遇到的问题是更新(PUT)现有实体。流程如下所示:
角度问题HTTP PUT传递给WebAPI方法,并将VM作为参数。
然后,WebAPI调用Service方法,将VM作为参数传递。
Services方法使用自动映射器将VM转换为EF实体
然后,服务方法调用相应的存储库,并传递分离的EF实体的新实例作为参数。
服务方法示例:
public async Task<List<DependantViewModel>> SaveDependants(int customerId, List<DependantViewModel> dependantViewModels)
{
var dependantEntities = Mapper.Map<List<DependantViewModel>, List<Dependant>>(dependantViewModels);
bool result = false;
foreach (var dependantEntity in dependantEntities)
{
result = await _dependantRepository.InsertOrUpdate(dependantEntity);
if (result != true)
{
// log errror
}
}
return Mapper.Map<List<Dependant>, List<DependantViewModel>>(dependantEntities);
}
BaseRepo:
public virtual async Task<bool> InsertOrUpdate(TE entity)
{
if (entity.Id == 0 || entity.Id == ModelState.New)
{
// insert new item
_context.Entry<TE>(entity).State = EntityState.Added;
}
else
{
// update existing item
_context.Entry<TE>(entity).State = EntityState.Modified;
_context.Entry<TE>(entity).Property(o => o.CreatedUserId).IsModified = false;
_context.Entry<TE>(entity).Property(o => o.CreatedDate).IsModified = false;
}
return await _context.SaveChangesAsync() > 0;
}
尝试设置时发生异常:
_context.Entry(entity).State = EntityState.Modified;
附加类型的实体
“ Business.Data.Entities.Customer.Dependant”失败,因为另一个
相同类型的实体已经具有相同的主键值。这个
使用“附加”方法或设置
如果图中的任何实体具有“未更改”或“已修改”的实体
关键值冲突。这可能是因为某些实体是新的,
尚未收到数据库生成的键值。在这种情况下使用
“添加”方法或“添加”实体状态来跟踪图形和
然后将非新实体的状态设置为“未更改”或“已修改”,如下所示:
适当。
据我了解,发生这种情况是因为在设置实体状态之前,需要先附加分离的实体。问题是,我无法附加实体,因为它已经在context.Dependant.local集合中。由于某种原因,实体框架已经在跟踪实体。
有没有一种方法可以先检查上下文中是否存在实体,如果不存在,则附加,否则,检索已附加的实体,然后将修改后的实体中的更改应用于附加的实体(如果有意义) 。
感谢任何反馈
参考方案
这对我有用:
public virtual async Task<bool> InsertOrUpdate(TE entity)
{
if (entity.Id == 0 || entity.Id == ModelState.New)
{
// insert new item
_context.Entry<TE>(entity).State = EntityState.Added;
}
else
{
var attachedEntity = _context.ChangeTracker.Entries<TE>().FirstOrDefault(e => e.Entity.Id == entity.Id);
if (attachedEntity != null)
{
// the entity you want to update is already attached, we need to detach it and attach the updated entity instead
_context.Entry<TE>(attachedEntity.Entity).State = EntityState.Detached;
}
_context.Entry<TE>(entity).State = EntityState.Modified; // Attach entity, and set State to Modified.
_context.Entry<TE>(entity).Property(o => o.CreatedUserId).IsModified = false;
_context.Entry<TE>(entity).Property(o => o.CreatedDate).IsModified = false;
}
return await _context.SaveChangesAsync() > 0;
}
如何排序List <Entity>? - c#假设我有一个列表:MyList = new List<MyEntity>(); 然后我尝试将项目添加到列表中。 WCF RIA服务通过异步调用加载的每个项目。在添加任何新项目之后,我希望MyList始终按MyEntity的属性进行排序,例如ID。如何解决这个问题? 参考方案 您可以使用SortedSet使其保持排序 SortedSet<in…
将谓词<T>转换为Func <T,bool> - c#我有一个包含成员Predicate的类,希望在Linq表达式中使用该类:using System.Linq; class MyClass { public bool DoAllHaveSomeProperty() { return m_instrumentList.All(m_filterExpression); } private IEnumerable&…
Java中的<<或>>>是什么意思? - javaThis question already has answers here: Closed 7 years ago. Possible Duplicate: What does >> and >>> mean in Java?我在一些Java代码中遇到了一些陌生的符号,尽管代码可以正确编译和运行,但对于括号在此代码中的作用却感…
菱形运算符<>是否等于<?> - java我在util.TreeSet类中发现,其中一个构造函数正在使用具有空泛型类型的新TreeMap调用另一个构造函数。 public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } new TreeMap<>是什么意思…
通过Maven编译器插件不会发生有限的包含和排除 - java我正在使用3.6.0版的maven编译器插件,在此我们只想在特定文件夹中编译一个文件,而在该位置编译所有其他文件。例如:在文件夹应用程序中有14个文件,从那我只希望编译1个文件,但它编译了所有文件,如果我要排除,则它也不起作用。 <sourceDirectory>${basedir}/../src/java</sourceDirectory…