views:

58

answers:

2

In my application I have an entity which is being used essentially as a complex many to many between my User and Project entities. I am trying to determine how to figure out if my service layer needs to add the entity to the context or attach the entity (for updating an existing entity) and I am at a loss of how.

This is easy to determine for most of my entities because of the Int Id field, if it's zero the database hasn't given it an identity value yet. This is not possible if it's a composite primary key.

Does anyone have any suggestion on how to determine if an instance of an entity is new or an update to an existing record?

Edit: I forgot to mention, these entities are POCOs built for code-first, so I don't have an EntityState property on the entity itself.

+1  A: 
if (x.EntityState == System.Data.EntityState.Added)
    //Add
else if (x.EntityState == System.Data.EntityState.Modified)
    //Attach

for more info

http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx

franklins
`EntityState` is a `FlagsAttribute`; it can *contain* `Added` without being *equal* to it.
Craig Stuntz
+3  A: 

Yes, as the above answers state, you check the EntityState for the entity in the OSM.

However, keep in mind this only works for entities attached to the context/graph.

I'm currently working with detached entities (ASP.NET MVC), and because they are not attached to the graph, the EntityState is unchanged.

In this case, i am doing a precautionary call to the DB to grab the entity by the key. If nothing is returned, i do an Add, otherwise i use ApplyCurrentValues to override the values, then do .SaveChanges

I'm still wondering if this is the correct way, but thought i'd put it out there.

I'm using POCO's which have no change tracking, hence i have to do a bit more work.

Since there is no EntityState for the POCO, you have to manually call into the OSM:

var pocosInGraph = ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified)

After you Attach/Add, your POCO should be in that collection.

As i said though, if this is for a MVC application, your entities are detached on a HTTP POST, and thus the EntityState will still be unchanged.

In my case, i manually set the EntityState after Attaching:

ctx.Attach(poco);
ctx.ObjectStateManager.ChangeObjectState(poco, EntityState.Modified);
ctx.SaveChanges();
RPM1984
Thanks for that. I'm using ASP.NET MVC as well, so I guess checking with the DB before attaching or adding is the way to go.
KallDrexx
Cool. Yeah i was noticing if i forgot to bind all properties in my View, it was seeing this as a flag to "clear" these values when i submit the form. I think it's safer to do the call (it's an efficient call anyway).
RPM1984