最有效地处理创建,更新,删除与实体框架代码首先 [英] Most efficiently handling Create, Update, Delete with Entity Framework Code First
问题描述
注意:我正在使用Entity Framework版本5
Note: I am using Entity Framework version 5
里面我的通用存储库,我有添加
,编辑
和删除
方法如下:
Inside my generic repository, I have Add
, Edit
and Delete
methods as below:
public class EntityRepository<T> : IEntityRepository<T>
where T : class, IEntity, new() {
readonly DbContext _entitiesContext;
public EntityRepository(DbContext entitiesContext) {
if (entitiesContext == null) {
throw new ArgumentNullException("entitiesContext");
}
_entitiesContext = entitiesContext;
}
//...
public virtual void Add(T entity) {
DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
if (dbEntityEntry.State != EntityState.Detached) {
dbEntityEntry.State = EntityState.Added;
}
else {
_entitiesContext.Set<T>().Add(entity);
}
}
public virtual void Edit(T entity) {
DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
if (dbEntityEntry.State == EntityState.Detached) {
_entitiesContext.Set<T>().Attach(entity);
}
dbEntityEntry.State = EntityState.Modified;
}
public virtual void Delete(T entity) {
DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
if (dbEntityEntry.State != EntityState.Detached) {
dbEntityEntry.State = EntityState.Deleted;
}
else {
DbSet dbSet = _entitiesContext.Set<T>();
dbSet.Attach(entity);
dbSet.Remove(entity);
}
}
}
你觉得这些方法实施得很好特别是添加
方法。执行添加
方法是否更好?
Do you think whether these methods are well implemented? Especially the Add
method. Would it be better to implement the Add
method as below?
public virtual void Add(T entity) {
DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
if (dbEntityEntry.State == EntityState.Detached) {
_entitiesContext.Set<T>().Attach(entity);
}
dbEntityEntry.State = EntityState.Added;
}
推荐答案
public bool Add<E>(E entity) where E : class
{
DataContext.Entry(entity).State = System.Data.EntityState.Added;
Save();
}
要更新:
public bool Update<E>(E entity) where E : class
{
DataContext.Entry(entity).State = System.Data.EntityState.Modified;
Save();
}
删除:
public bool Delete<E>(E entity) where E : class
{
DataContext.Entry(entity).State = System.Data.EntityState.Deleted;
Save();
}
另有私人 Save()
方法返回true或false,这样你可以在控制器中轻松地根据结果
And a private Save()
method that returns true or false so you can fallback easy in the controller depending on the result
private bool Save()
{
return DataContext.SaveChanges() > 0;
}
这只是我的通用存储库的一部分。
This is only a portion of my generic repository. It works great in enterprise applications.
更新:
Detach仅影响传递给该方法的特定对象。如果分离的
对象在对象上下文中有相关对象,那么
对象就不会分离。
Detach only affects the specific object passed to the method. If the object being detached has related objects in the object context, those objects are not detached.
当设置实体的状态或调用 SaveChanges()
时,EF将自动附加图形中的分离对象。
EF will automatically attach detached objects in the graph when setting the state of an entity or when SaveChanges()
is called.
我真的不知道为什么你需要从上下文中分离对象。您也可以使用 AsNoTracking()
从数据库加载实体,而不必首先将它们附加到上下文。
I really don't know why you need to detach objects from the context. You can also use AsNoTracking()
to load entities from the database without attaching them to the context in the first place.
这篇关于最有效地处理创建,更新,删除与实体框架代码首先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!