Can I name the "StateID" property for an object in EF4.0?

I am trying to update an entity in a database using EF4.0, so I am doing this:

_dbctx.AddObject("MyEntities", EntityWithUpdates); _dbctx.ObjectStateManager.ChangeObjectState(EntityWithUpdates, EntityState.Modified); _dbctx.SaveChanges(); 

MyEntities has a property called StateID. Using debug, I can see the correct value in the StateID 217 property when it is added to the context, but the value somehow changed to 0 when ObjectStateManager.ChangeObjectState shuts down.

Has anyone seen this before?

This is the only property that has a problem. Should the StateID property name be excluded or is there a job?

My essence is as follows:

 public class MyEntity { public int ID { get; set; } public int StateID { get; set; } } 

If I get the object from the database first. Then apply the current values, all this works fine:

 MyEntity entity = _dbctx.MyEntities.Where(i => i.ID == id).FirstOrDefault(); _dbctx.ApplyCurrentValues("MyEntities", EntityWithUpdates); _dbctx.SaveChanges(); 

This seems to be an object graph problem, but I still prefer to be able to attach the object and save to avoid calling db.

I looked at several other objects in the application and I believe that eran otzap is correct. The entire object graph is saved along with the state. Since the state is not loaded, EF wants to create it, even if it has a valid identifier. Therefore, it seems that the only safe way to apply changes to this object is to first get it from the subsequent db, apply it, and then save the changes. Bummer.

For everyone who can see it. eran otzap suggests to apply value for StateID. He said this β€œmay” work. In my situation this is not so. I had to get the object first before EF recognized the StateID.

+5
source share

All Articles