当我迭代通过ObjectStateEntries我期望[t]变量名称将是MY_ENTITY
foreach (ObjectStateEntry entry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
{
Type t = entry.Entity.GetType();
...
}
Run Code Online (Sandbox Code Playgroud)
但我是真实的
System.Data.Entity.DynamicProxies.MY_ENTITY_vgfg7s7wyd7c7vgvgv.....
Run Code Online (Sandbox Code Playgroud)
如何确定可以将当前条目转换为MY_ENTITY类型?
c# entity-framework objectstatemanager ef-code-first dbcontext
我试图将一个实体附加到ObjectContext.当我这样做时,抛出以下InvalidOperationException:
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same key.
Run Code Online (Sandbox Code Playgroud)
我检查了对象状态管理器,该项目不存在:
//Data context is actually the object context.
ObjectStateEntry contact;
while ( //Should only work once since it should be true if the item was attached
!DataContext.ObjectStateManager.
TryGetObjectStateEntry(Contact, out contact)
)
DataContext.Attach(Contact); //Here is the exception thrown.
Run Code Online (Sandbox Code Playgroud)
或者看看这个抽象的例子并告诉我它是否有意义:
EntityState state = Contact.EntityState; //Detached
DataContext.Attach(Contact); //Throws the exception.
DataContext.AttachTo("Entities.Contacts", Contact); //Throws the Exception
var detached = DataContext.ObjectStateManager.
GetObjectStateEntries(EntityState.Detached);
//InvalidArgumentException …Run Code Online (Sandbox Code Playgroud) 如何更新数据存储中的所有脏实体,并将更改后的值重置为原始存储值?
ObjectContext.Refresh方法需要刷新实体作为参数.
entity-framework refresh reload dirty-data objectstatemanager
如果我修改POCO实体的属性,但重置它,EntityFramework仍然表示存在更改.
Property "Name": Value "Test" (original value)
-> Value "Test123" (value changed by UI)
-> Value "Test" (value changed by UI to original value)
Run Code Online (Sandbox Code Playgroud)
已修改的条目:
var objectStateEntries =
_db.ObjectStateManager.GetObjectStateEntries(
EntityState.Added |
EntityState.Deleted |
EntityState.Modified);
Run Code Online (Sandbox Code Playgroud)
你怎么处理这个案子?
c# entity-framework change-tracking objectstatemanager entity-framework-4
我有以下HttpPostDelete() 方法,它在我的 MVC5 应用程序中工作。据我所知,与此控制器、视图甚至模型相关的任何内容都没有发生变化。
// POST: Admin/UserManagement/Delete/5
[HttpPost, ActionName("DeleteConfirmed")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(string id)
{
ApplicationUser applicationUser = db.Users.Find(id);
if (applicationUser == null)
{
ModelState.AddModelError("", "Failed to find User ID for deletion.");
}
else
{
IdentityResult result = await UserManager.DeleteAsync(applicationUser);
if (result.Succeeded)
{
await db.SaveChangesAsync();
return RedirectToAction("Index", "UserManagement");
}
else
{
ModelState.AddModelError("", "Failed to Delete User.");
var errors = string.Join(",", result.Errors);
ModelState.AddModelError("", errors);
}
}
return View(applicationUser);
}
Run Code Online (Sandbox Code Playgroud)
用户管理器部分:
[CustomAuthorization(myRoles = "Admin")]
public class UserManagementController : Controller …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc objectstatemanager entity-framework-6 asp.net-mvc-5