Is is possible to check if an object is already attached to a data context in Entity Framework?
A simpler approach is:
bool isDetached = context.Entry(user).State == EntityState.Detached;
if (isDetached)
context.Users.Attach(user);
Here's what I ended up with, which works very nicely:
public static void AttachToOrGet<T>(this ObjectContext context, string entitySetName, ref T entity)
where T : IEntityWithKey
{
ObjectStateEntry entry;
// Track whether we need to perform an attach
bool attach = false;
if (
context.ObjectStateManager.TryGetObjectStateEntry
(
context.CreateEntityKey(entitySetName, entity),
out entry
)
)
{
// Re-attach if necessary
attach = entry.State == EntityState.Detached;
// Get the discovered entity to the ref
entity = (T)entry.Entity;
}
else
{
// Attach for the first time
attach = true;
}
if (attach)
context.AttachTo(entitySetName, entity);
}
You can call it as follows:
User user = new User() { Id = 1 };
II.AttachToOrGet<Users>("Users", ref user);
This works very nicely because it's just like context.AttachTo(...)
except you can use the ID trick I cited above each time. You end up with either the object previously attached or your own object being attached. Calling CreateEntityKey
on the context makes sure it's nice and generic and will work even with composite keys with no further coding (because EF can already do that for us!).
Edit, twelve years later (Dec 2021)... oof!
Here's what I use in EF Core:
public static class EfExtensions
{
public static T AttachToOrGet<T>(this DbContext context, Func<T,bool> predicate, Func<T> factory)
where T : class, new()
{
var match = context.Set<T>().Local.FirstOrDefault(predicate);
if (match == null)
{
match = factory();
context.Attach(match);
}
return match;
}
}
Usage:
var item = db.AttachToOrGet(_ => _.Id == someId, () => new MyItem { Id = someId });
You could refactor this to work with the entity key but this is enough to get anyone going!