entity framework updates are silently ignored - entity-framework

I retrieve a stay via a WCF service and pass it to the front end. A user updates the data on that object and then its passed back to the WCF service.
The following code works fine to add new stays, but it silently ignores updates. What am I missing?
public void UpdateStay(ResidentDataTypes.Stay stay)
{
using (ResidentDataTypes.ResidentEntities entity = new ResidentDataTypes.ResidentEntities())
{
if (stay.StayId == Guid.Empty)
{
entity.Stay.AddObject(stay);
}
else
{
entity.Stay.ApplyChanges(stay);
}
entity.SaveChanges();
}
}

When your objects serialized from WCF they are in Detached state and EF will not generate update statement for them. You have 2 options:
1. Get the same object from the DB and then do a ObjectSet.ApplyCurrentValues:
using (ResidentDataTypes.ResidentEntities context = new
ResidentDataTypes.ResidentEntities())
{
if (stay.StayId == Guid.Empty)
{
context.Stay.AddObject(stay);
}
else
{
// Fetch the Stay object into the cache:
context.Stay.First(s => s.StayId == stay.StayId);
// Now ApplyCurrentValues from the stay object coming from WCF:
context.Stay.ApplyCurrentValues(stay);
}
entity.SaveChanges();
}
2.Manually change the Detached state to Modified:
using (ResidentDataTypes.ResidentEntities context = new
ResidentDataTypes.ResidentEntities())
{
if (stay.StayId == Guid.Empty)
{
context.Stay.AddObject(stay);
}
else
{
// Attach the stay object coming from WCF to ObjectContext:
context.Stay.Attach(stay);
// The attached object is going into Unchanged mode after attaching
// so we need to change the state to Modified:
context.ObjectStateManager.ChangeObjectState(stay, EntityState.Modified);
}
entity.SaveChanges();
}

i think you have to attach your entity to your context with something like
entity.Attach(stay)

Related

Entity framework core - DbUpdateException

UPDATED: I have a piece of code that creates records when they don't exist, or updates them when they do exist. However, while trying to update the records I get this exception:
Microsoft.EntityFrameworkCore.DbUpdateException The DELETE statement
conflicted with the REFERENCE constraint
public static string AddCurrencies(ApplicationDbContext db)
{
// ...
foreach (Currency c in db.Currency.ToList())
{
try
{
db.Remove(c); // the troublemaker!
db.SaveChanges();
}
catch
{
// probably in use (foreign key)
}
}
// ...
foreach (Currency c in CurrencyList)
{
var c_db = db.Currency.FirstOrDefault(x => x.Code == c.Code);
if (c_db == null)
{
// adding
db.Currency.Add(c);
}
else
{
// updating
c_db.Name = c.Name;
c_db.LocalDisplay = c.LocalDisplay;
}
db.SaveChanges(); // exception fired if updating!
}
// ...
}
After some investigation, and having being able to turn SQL debugging on, I found out that the Remove() "persists" and that it will be retried with the second call to SaveChanges(), hence the exception. Now the question is reformulated: how do I "undo" (in the lack of a better expression) the Remove() commands that failed?
I managed to solve this issue this way:
var entry = context.Entry(entity);
entry.Reload();
for each entry where delete failed.

How to add a delete callback in entity framework?

Inspired by ruby on rails I want to add a delete callback to entity framework. I've started by overriding SaveChanges() to loop over the tracked entities and create an interface which is called whenever an entity gets deleted.
var changedEntities = ChangeTracker.Entries();
foreach (var changedEntity in changedEntities)
{
if (changedEntity.Entity is IBeforeDelete && changedEntity.State == EntityState.Deleted)
{
IBeforeDelete saveChange = (IBeforeDelete)changedEntity.Entity;
saveChange.BeforeDelete(this, changedEntity);
}
}
This works quite well, but I figured one problem and I don't know how to solve that. If an entity gets deleted within the callback, the Changetracker needs to be updated to resprect the newly deleted items. How can I solve that? Or is there another solution? Or do I do it wrong?
Good question. If I understand you correctly, your BeforeDelete implementations might delete a different entry that also needs to have BeforeDelete called on it. This could recursively go forever, so the only thing I could think of would be to recursively check the change tracker entries to see if new ones were added after the last batch was processed.
Untested:
public override int SaveChanges()
{
var processed = new List<DbEntityEntry>();
var entries = ChangeTracker.Entries();
do
{
foreach (var entry in entries)
{
processed.Add(entry);
if (entry.Entity is IBeforeDelete && entry.State == EntityState.Deleted)
{
IBeforeDelete saveChange = (IBeforeDelete)entry.Entity;
saveChange.BeforeDelete(this, entry);
}
}
} while ((entries = ChangeTracker.Entries().Except(processed)).Any());
return base.SaveChanges();
}
you can filter out uncommited entity changes...
var changedEntities = Context.ChangeTracker
.Entries<TEntity>()
.Where(e => e.State != EntityState.Detached)
.Select(e => e.Entity);
and lock on the rest in your "if" block
lock(changedEntity.Entity){ ... interface code }

TransactionScope with two datacontexts doesn't roll back

I am trying to solve situation with rolling back our datacontexts.
We are using one TransactionScope and inside two data contexts of two different databases.
At the end we want to save changes on both databases so we call .SaveChanges but the problem is that when an error occurs on the other database the changes on the first database are still saved.
What am I doing wrong in there that the first database doesn't roll back?
Thank you,
Jakub
public void DoWork()
{
using (var scope = new TransactionScope())
{
using (var rawData = new IntranetRawDataDevEntities())
{
rawData.Configuration.AutoDetectChangesEnabled = true;
using (var dataWareHouse = new IntranetDataWareHouseDevEntities())
{
dataWareHouse.Configuration.AutoDetectChangesEnabled = true;
... some operations with the data - no savechanges() is being called.
// Save changes for all items.
if (!errors)
{
// First database save.
rawData.SaveChanges();
// Fake data to fail the second database save.
dataWareHouse.Tasks.Add(new PLKPIDashboards.DataWareHouse.Task()
{
Description = string.Empty,
Id = 0,
OperationsQueue = new OperationsQueue(),
Queue_key = 79,
TaskTypeSLAs = new Collection<TaskTypeSLA>(),
Tasktype = null
});
// Second database save.
dataWareHouse.SaveChanges();
scope.Complete();
}
else
{
scope.Dispose();
}
}
}
}
From this article http://blogs.msdn.com/b/alexj/archive/2009/01/11/savechanges-false.aspx
try to use
rawData.SaveChanges(false);
dataWareHouse.SaveChanges(false);
//if everything is ok
scope.Complete();
rawData.AcceptAllChanges();
dataWareHouse.AcceptAllChanges();

EF: EnsureLoadedForContext method

I am reading EF's source code and found this method below. According to the method name, it make sure that the dbcontext is loaded. When I test this with EF Codefirst sample, this method is added the current assembly (my sample console) to "_knownAssemblies"..
I don't see any code of loading the assembly. And I don't see any code that checks whether the assembly is loaded or not.
Is that the naming issue or Did I miss out something? Thanks in advance.
public virtual void EnsureLoadedForContext(Type contextType)
{
DebugCheck.NotNull(contextType);
Debug.Assert(typeof(DbContext).IsAssignableFrom(contextType));
var contextAssembly = contextType.Assembly;
if (contextType == typeof(DbContext)
|| _knownAssemblies.ContainsKey(contextAssembly))
{
return;
}
if (_configurationOverrides.IsValueCreated)
{
lock (_lock)
{
if (_configurationOverrides.Value.Count != 0)
{
return;
}
}
}
if (!ConfigurationSet)
{
var foundConfigurationType =
_loader.TryLoadFromConfig(AppConfig.DefaultInstance) ??
_finder.TryFindConfigurationType(contextType);
if (foundConfigurationType != null)
{
SetConfigurationType(foundConfigurationType);
}
}
else if (!contextAssembly.IsDynamic // Don't throw for proxy contexts created in dynamic assemblies
&& !_loader.AppConfigContainsDbConfigurationType(AppConfig.DefaultInstance))
{
var foundType = _finder.TryFindConfigurationType(contextType);
if (foundType != null)
{
if (_configuration.Value.Owner.GetType() == typeof(DbConfiguration))
{
throw new InvalidOperationException(Strings.ConfigurationNotDiscovered(foundType.Name));
}
if (foundType != _configuration.Value.Owner.GetType())
{
throw new InvalidOperationException(
Strings.SetConfigurationNotDiscovered(_configuration.Value.Owner.GetType().Name, contextType.Name));
}
}
}
_knownAssemblies.TryAdd(contextAssembly, null);
}
The method EnsureLoadedForContext does not load the context but loads a configuration for the context type passed to the method. When you look at the name of the method with the type name on which the method is created (DbConfigurationManager.EnsureLoadedForContext) it is much more clear that the method is related to loading a configuration rather than loading a context. Finally you can look at a comment to one of the bugs which reads:
EnsureLoadedForContext is called from various places as soon as a context type is known to ensure that the correct DbConfiguration is found.

Understanding Entity Framework optimistic concurrency (database wins) pattern

See Resolving optimistic concurrency exceptions with Reload (database wins) :
using (var context = new BloggingContext())
{
var blog = context.Blogs.Find(1);
blog.Name = "The New ADO.NET Blog";
bool saveFailed;
do
{
saveFailed = false;
try
{
context.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
// Update the values of the entity that failed to save from the store
ex.Entries.Single().Reload();
}
} while (saveFailed);
}
Why the method SaveChanges() is called after Reload()?
This call will never change the data in the database.
I agree it's not too clear. The intention of this piece of code is in the sentence
The entity is then typically given back to the user in some form and they must try to make their changes again and re-save.
So it would have been better if they had added a comment:
...
// User evaluates current values and may make new changes.
try
{
context.SaveChanges();
}
...