Reputation: 36573
I need to check if an entity is new. If it was an int/identity primary key, I could just check if the property for the key has a default value...but in the case of a Guid, I can't do this. Is there anything I can do with the ObjectContext or ObjectStateManager to have it check and determine if the entity in question is new vs. modified?
Upvotes: 1
Views: 387
Reputation: 4966
ObjectStateManager.GetObjectStateEntry
using (MyContext context = new MyContext())
{
ObjectStateManager objectStateManager = context.ObjectStateManager;
EntityState state = objectStateManager.GetObjectStateEntry(obj).State;
}
Upvotes: 2
Reputation: 27441
If it's a nullable Guid, you can check if it has a value:
if (entity.MyGuid.HasValue)
{
// hooray!
}
If it's not nullable, check for default value or an empty GUID:
if (entity.MyGuid != default(Guid))
{
// hooray!
}
if (entity.MyGuid != Guid.Empty)
{
// hooray!
}
Upvotes: 0
Reputation: 1038730
In the case of a Guid you can do this
if(foo.GuidProperty == default(Guid))
{
}
Upvotes: 0