Question

Is it possible to execute the MERGE in T-SQL command using the.NET Entity Framework 4?

1
11
12/3/2014 4:34:44 PM

Accepted Answer

No, there isn't any such built-in feature; you have to create it yourself. For instance, a method like:

public void SaveOrUpdate(MyEntity entity)
{
    if (entity.Id == 0)
    {
        context.MyEntities.AddObject(entity);
    }
    else
    {
        context.MyEntities.Attach(entity);
        context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
    }

    // You can call SaveChanges here or you can call it separately after multiple changes
}

This is an example of how to interact with a detached entity that hasId created automatically in the database (IDENTITY Since the actual value will be given while storing modifications, the default Id for new entities is always 0.

11
4/30/2011 2:57:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow