When does the entity get detached in Entity Framework? - entity-framework

I have seen in many posts that if the tracking is not enabled, the entity is detached.
What I would like to know is: how can there be few objects which are not tracked and a few which are tracked?
Can someone share the code snippet which shows that this entity is not tracked by a context.
According to MSDN:
Detached: the entity is not being tracked by the context
https://msdn.microsoft.com/en-us/library/jj592676(v=vs.113).aspx
According to following post which I read:
http://blog.maskalik.com/entity-framework/2013/12/23/entity-framework-updating-database-from-detached-objects/
var entry = _context.Entry<T>(entity);
if (entry.State == EntityState.Detached)
{
_context.Set<T>().Attach(entity);
entry.State = EntityState.Modified;
}
Detached objects, or objects that are created outside of Entity Framework (EF), don’t have automatic tracking enabled.
And creating a POCO class in code-first approach is one such example of a detached entity.
Is this the only scenario?

There are more scenarios for the Detached Object.
1.You dont want to track an entity.
var entity= context.MyEntities.AsNoTracking().Where(...).FirsOrDefault();
In this query entities retrieved are not tracked hence any changes on the entities will not be recorded to database.
Consider this.
entity.Name = "1";
context.SaveChanges();
As this entities are not tracked the changes will not be saved
unless you attach this.
var entry = _context.Entry<T>(entity);
if (entry.State == EntityState.Detached)
{
_context.Set<T>().Attach(entity);
entry.State = EntityState.Modified;
}
2.Consider your working on disconnected architecture (API,Web). Consider an employee API which have PUT endpoint.
This would attach the employee to the context and update the entity as context is not aware of this entity.
Advantage : No need to fetch the employee entity from the database.
Disadvantage : Someother user changes the entity between the transaction might be losed (you can still update property that are only changed)
public void UpdateEmployee(Employee entity)
{
var entry = _context.Entry<Employee>(entity);
if (entry.State == EntityState.Detached)
{
_context.Attach(entity);
entry.State = EntityState.Modified;
}
Context.SaveChanges()
}
Second Version
public void UpdateEmployee(Employee entity)
{
var dbItem = context.EmployeeEnities.FirstOrDefault(g=>g.Id==entity.Id);
//Context is already have track of this entity, you can just update properties you have changed.
dbItem.Name = entity.Name;
Context.SaveChanges()
}

Related

EF 6 OriginalValues lost when using Attach method

I have the following Update generic method for my entities:
public void Update < T > (T entity) where T: class {
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State == System.Data.Entity.EntityState.Detached) {
DbContext.Set < T > ().Attach(entity);
}
dbEntityEntry.State = System.Data.Entity.EntityState.Modified;
}
After SaveChanges() the data is successfully updated in the DB.
Now I nee to implement and Audit Log before SaveChanges() but I noticed that CurrentValues are equal to OriginalValues:
// For updates, we only want to capture the columns that actually changed
if (!object.Equals(dbEntry.OriginalValues.GetValue<object>(propertyName), dbEntry.CurrentValues.GetValue<object>(propertyName))){
//here I add a new Audit Log entity
}
Any clue on how to solve this? Or is there a better way to do it in Entity Framework 6?
If you are using a disconnected entity, you can set originals values without affect entity instance values, adapt this method at you needs
public static void LoadOriginalValues(this WorkflowsContext db, DbEntityEntry entity)
{
var props = entity.GetDatabaseValues();
foreach (var p in props.PropertyNames)
{
if (entity.Property(p).IsModified)
{
entity.Property(p).OriginalValue = props[p];
}
}
}
The original values are recovered from the entity itself. If the entity is being tracked by a context, this information is available.
In your case, you're using a disconected entity, so there is no change tracking, and the entity doesn't have the original values.
SO, in this case, if you need the original values there is no other option than getting them from the DB, and compare them, one by one.
If you want to get an entity that behaves as if it had been tracked by the context you can use a context to read the entity from the DB, and use something like ValueInjecter to automatically set the property values from the disconected entity into the tracked entity.

Updating a related Entity tries to add a new related Entity

When updating myItem1 the related entity Entity2 doesn't update but EF tries to add a new Entity2. It throws a primary key constraint error. MyItem has a many-to-one relationship with Entity2
public HttpResponseMessage PutMyItem(MyItem myitem)
{
if (ModelState.IsValid)
{
MyItem myItem1 = db.MyItems.First(m => m.MyItemId == myitem.MyItemId);
myItem1.Name = myitem.Name;
myItem1.Entity2 = myitem.Entity2;
db.ObjectStateManager.ChangeObjectState(myItem1, EntityState.Modified);
try
{
db.SaveChanges();
}
Looks like this line attaches Entity2 to the context in the Added state. Setting the state of myItem to Modified doesn't affect its child entities...
myItem1.Entity2 = myitem.Entity2;
If you are sure this is a valid entity that already exists in the database, change its state to Modified...
db.ObjectStateManager.ChangeObjectState(myItem1.Entity2, EntityState.Modified);
Also, there shouldn't be a need to mark myItem1 as Modified, as it should already in Modified state when its Name property was set.

How do I prevent EF from reinserting objects of other dbcontext?

I am using EF 4.1 in a MVC 3 project and I am having problems with inserting an object. I am using session as well to hold onto some objects. In concrete :
I have a simple class with a parent child relationship:
public class Event
{
public User Promotor {get;set;}
}
The promotor is based on the CurrentUser. In my application I store the CurrentUser in http session. Now when I add an event like this the user (and all related objects) gets inserted one more time with a new primary key.
//1st request inserts/loads the user
User user;
using (var context = new MyDbContext())
{
user = new User();
context.Users.Add(user);
context.SaveChanges();
}
//2nd request saves the event
var before = db.Users.Count();
var #event = new Event
{
Promotor = user, //user was kept in Session
};
db.Entry(#event).State = EntityState.Added;
db.SaveChanges();
When i check the state of the user it is 'added' as well although the primary key is not 0 and EF should know it is already persistent. How can fix this without adding a lot of to my persistency code. Do I have to reattach my currentuser to the new dbcontext on every request? This will lead to db code 'leaking' into my application. I want to keep the DB stuff in a data layer. I am using a repository like this :
public void Save(T entity)
{
dbContext.Entry(entity).State = IsPersistent(entity) ?
EntityState.Modified : EntityState.Added;
dbContext.SaveChanges();
}
As you've already mentioned yourself, reattaching the user to your current context should solve the problem. The only other way I know of, would be to retrieve the object once again in your context based on the primary key value.

Getting the most out of Entity Framework in a ASP MVC web application

How do I get the most out of my Entity Framework (v4.0 or better) in my ASP MVC3 web application? My main problem is due to the request-response nature of the web, it seems I have to manually track objects being displayed on a form to the DB in order to do CUD operations. Eg. as suggested in Editing and Updating Entity Framework entity in ASP .NET MVC this seems awfully manual. Is there a way to keep my context in my session some how such that EF is doing all the work for me?
Don't store ObjectContext in session. Use a new context for each request processing. Here you can read something about long living contexts. If you use long living context stored in session you will have a big problem to load fresh data. Also if your user opens your application in multiple browser tabs (= same session) you can get some very unexpected results.
If you want to update just scalar values (no changes in navigation properites) you can use:
Insert scenario:
[HttpPost]
public ActionResult Insert(MyEntity entity)
{
using (var context = new MyContext())
{
context.MyEntities.AddObject(entity);
context.SaveChanges();
}
...
}
Update scenario with fully detached object:
[HttpPost]
public ActionResult Update(MyEntity entity)
{
using (var context = new MyContext())
{
context.MyEntities.Attach(entity);
context.ObjectStateManager.ChangeObjecState(entity, EntityState.Modified);
context.SaveChanges();
}
...
}
Update scenario with loading object first from DB:
[HttpPost]
public ActionResult Update(MyEntity entity)
{
using (var context = new MyContext())
{
int id = entity.Id;
context.MyEntities.Single(e => e.Id == id); // You must load the record first
context.MyEntities.ApplyCurrentValues(entity);
context.SaveChanges();
}
...
}
Delete scenario:
[HttpPost]
public ActionResult Delete(int id)
{
using (var context = new MyContext())
{
var entity = context.MyEntities.Single(e => e.Id == id);
context.MyEntities.DeleteObject(entity);
context.SaveChanges();
}
...
}
It is possible to delete the entity without loading it first but if the entity has any relation it couses a lot of troubles.
If you want to modify relations as well it can involve using either UpdateMode / TryUpdateModel as discussed here or tracking changes manually as described here. Simple relation updates on Foreign key association (the description is the same even if you don't use code-first) can be still handled by previous examples.

MVC 2 and EF4 Self-tracking entities models have bad state on post back

I've got standard Create() Edit() and Delete() methods on my controllers, and I am using the EF4 Self-tracking entities.
When the edit is posted back, the model.ChangeTracker.ChangeTracking = false, and model.ChangeTracker.State = ObjectState.Added, even though I made sure those are set when retrieving the record initially.
Are the self-tracking entities not persisting the ChangeTracker class when the form is submitted? If so, how do I fix that?
public virtual ActionResult Edit(int personId)
{
IContext context = ContextFactory.GetContext();
EntityRepo Repo = new EntityRepo(context);
Person d = Repo.Person.GetById(PersonId);
d.ChangeTracker.ChangeTrackingEnabled = true;
return View(d);
}
[HttpPost]
public virtual ActionResult Edit(int personId, Person item)
{
try
{
if (ModelState.IsValid)
{
IContext context = ContextFactory.GetContext();
EntityRepo Repo = new EntityRepo(context);
// the item is returning these properties that are wrong
//item.ChangeTracker.ChangeTrackingEnabled = false;
//item.ChangeTracker.State = ObjectState.Added;
Repo.Person.Update(item);
Repo.Person.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
}
return View();
}
Let's start at the beginning.
What are Self-Tracking Entities, exactly?
A Self-Tracking Entity is an entity which can do change tracking even when it is not connected to a ObjectContext. They are useful in times when you must change the entity, but cannot have it connected to an ObjectContext.
So when would I want one, really?
Mostly, when you must have distributed objects. For example, one use case is when you are making a web service which talks to a Silverlight client. However, other tools, like RIA Services may be a better fit here. Another possible use case is for a long-running task. Since an ObjectContext is intended to be a unit of work and should typically not be long-lived, having a disconnected entity might make sense here.
Do they make any sense for MVC?
Not really, no.
Let's look at this a little deeper, and examine what happens when you update an entity in MVC. The general process is like this:
The browser issues a GET request for an update page.
The MVC app fetches an entity, and uses it to build an update HTML page. The page is served to the browser, and most C# objects, including your entity, are disposed. At this point, you can restart the Web server, and the browser will never know the difference.
The browser issues a POST request to update the entity.
The MVC framework uses the data in the POST in order to materialize an instance of an edit model which is passed to the update action. This might happen to be the same type as the entity, but it is a new instance.
The MVC app can update the entity and pass those changes back to the database.
Now, you could make self-tracking entities work by also including the full state of the STE in the HTML form and POSTing that back to the MVC app along with the scalar values on the entity. Then the Self-Tracking Entity might at least work.
But what benefit does this give you? The browser obviously cannot deal with your entity as a C# object. So it cannot make any changes to the entity worth tracking in terms that a Self-Tracking Entity would understand.
U should keep original STE in some hidden field. It's like your custom ViewState. In submit method u must merge original STE and new values.
Use ActionFilterAttribute for it.
Like
public class SerializeOriginalModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var viewResult = filterContext.Result as ViewResult;
if (viewResult == null)
return;
var viewModel = viewResult.ViewData.Model as ViewModel;
if (viewModel == null || viewModel.SteObject == null)
return;
byte[] bytes;
using (var stream = new MemoryStream())
{
var serializer = new DataContractSerializer(viewModel.SteObject.GetType());
serializer.WriteObject(stream, viewModel.SteObject);
bytes = stream.ToArray();
}
var compressed = GZipHelper.Compress(bytes);
viewModel.SerializedSteObject = Convert.ToBase64String(compressed);
}
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionParameters == null || filterContext.ActionParameters.Count == 0)
return;
var viewModel = filterContext.ActionParameters.First().Value as ViewModel;
var serialized = filterContext.HttpContext.Request.Form["SerializedSteObject"];
if (viewModel == null || String.IsNullOrEmpty(serialized))
return;
var type = filterContext.ActionParameters.First().Value.GetType().BaseType.GetGenericArguments()[0];
var bytes = GZipHelper.Decompress(Convert.FromBase64String(serialized));
using (var stream = new MemoryStream(bytes))
{
var serializer = new DataContractSerializer(type);
viewModel.SteObject = serializer.ReadObject(stream);
}
}
}
STE has one very big drawback. You have to store them in session or view state (WebForms). So it is nothing more than "new version of dataset". If you don't store STE you will have one instance for getting data and different for posting = no change tracking.
I think you are missing the idea of Repository. You should not have an Update method in the Repository. After submitting, you should get the item again, apply the modifications and then Save.
I prefer having a service layer between client and Repository. We can always change the strategy with which we merge.
And yes, if you need to persist your STE's between requests, use session or viewstate.
It should be
Repo.Person.ApplyChanges(item);
Repo.Person.SaveChanges();
instead of
Repo.Person.Update(item);
Repo.Person.SaveChanges();
Self Tracking works with ApplyChanges extention method.