Asp.net mvc 如何使用DbEntityEntry处理实体的状态?
我正在看John Papa的CodeCamper教程,在eRepository类中遇到了一段代码。我很难找到他为什么使用DbEntityEntry,当我查找DbEntityEntry.State属性的详细信息时,我发现他们一直在讨论一些图形,如: 设置实体状态或调用SaveChanges()时,EF将自动在图形中附加分离的对象 请告诉我这些是什么意思 下面是该类的外观:Asp.net mvc 如何使用DbEntityEntry处理实体的状态?,asp.net-mvc,entity-framework,entity-framework-4,linq-to-entities,entity-framework-5,Asp.net Mvc,Entity Framework,Entity Framework 4,Linq To Entities,Entity Framework 5,我正在看John Papa的CodeCamper教程,在eRepository类中遇到了一段代码。我很难找到他为什么使用DbEntityEntry,当我查找DbEntityEntry.State属性的详细信息时,我发现他们一直在讨论一些图形,如: 设置实体状态或调用SaveChanges()时,EF将自动在图形中附加分离的对象 请告诉我这些是什么意思 下面是该类的外观: public class EFRepository<T> : IRepository<T> where
public class EFRepository<T> : IRepository<T> where T : class
{
protected DbContext DbContext { get; set; }
protected DbSet<T> DbSet { get; set; }
public EFRepository(DbContext dbContext)
{
if (dbContext == null)
{
throw new ArgumentNullException("dbContext");
}
DbContext = dbContext;
DbSet = DbContext.Set<T>();
}
public virtual void Add(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != EntityState.Added)
{
dbEntityEntry.State = EntityState.Added;
}
else
{
DbSet.Add(entity);
}
}
public virtual void Update(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State == EntityState.Detached)
{
DbSet.Attach(entity);
}
dbEntityEntry.State = EntityState.Modified;
}
public virtual void Delete(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != EntityState.Deleted)
{
dbEntityEntry.State = EntityState.Deleted;
}
else
{
DbSet.Attach(entity);
DbSet.Remove(entity);
}
}
}
公共类eRepository:IRepository,其中T:class
{
受保护的DbContext DbContext{get;set;}
受保护的DbSet DbSet{get;set;}
公共eForepository(DbContext DbContext)
{
if(dbContext==null)
{
抛出新ArgumentNullException(“dbContext”);
}
DbContext=DbContext;
DbSet=DbContext.Set();
}
公共虚拟空添加(T实体)
{
DbEntityEntry DbEntityEntry=DbContext.Entry(实体);
if(dbEntityEntry.State!=EntityState.Added)
{
dbEntityEntry.State=EntityState.Added;
}
其他的
{
添加(实体);
}
}
公共虚拟无效更新(T实体)
{
DbEntityEntry DbEntityEntry=DbContext.Entry(实体);
if(dbEntityEntry.State==EntityState.Distached)
{
附加数据集(实体);
}
dbEntityEntry.State=EntityState.Modified;
}
公共虚拟作废删除(T实体)
{
DbEntityEntry DbEntityEntry=DbContext.Entry(实体);
if(dbEntityEntry.State!=EntityState.Deleted)
{
dbEntityEntry.State=EntityState.Deleted;
}
其他的
{
附加数据集(实体);
删除(实体);
}
}
}
如果您有一个WPF应用程序,那么您的实体将一直保留在内存中,以便您可以执行以下操作:
var cakeProduct = new Product{ProductName = "Cake"};
MyContext.ProductsDbSet.Add();
MyContext.SaveChanges();
和(一段时间后)
及
但是,如果你有一个ASP.NETMVC应用程序,你必须响应一个电话
[HttpPost]
public ActionResult Edit(Product someProduct){
//someProduct has only just been created in memory
var context = new MyContext(); //the Context does not exist yet
context.ProductsRepository.Update(someProduct);
}
MyContext.ProductsDbSet.Remove(cakeProduct);
MyContext.SaveChanges();
[HttpPost]
public ActionResult Edit(Product someProduct){
//someProduct has only just been created in memory
var context = new MyContext(); //the Context does not exist yet
context.ProductsRepository.Update(someProduct);
}