Reputation: 386
i need to calculate some values on the serverside when a specific entity gets updated. if i update the entity the following code gets executed (C_CompletePrice gets set) and it even gets reflected on clientside (clientside breeze gets all the properties nicely back) but when i check the db nothing is saved. so when clearing the browser cache and checking the entity again there are the old values...
private bool BeforeSaveTransaction(tblTransactions transaction, EntityInfo info)
{
transaction.C_CompletePrice = 11111111;
return true;
...
protected override bool BeforeSaveEntity(EntityInfo entityInfo)
{
var entity = entityInfo.Entity;
if (entity is tblTransactions)
{
return BeforeSaveTransaction(entity as tblTransactions, entityInfo);
}
...
i'm using breeze 1.4.6
on the server i'm using Breeze.WebApi and Breeze.WebApi.EF
the model i'm using: http://pastebin.com/Dc03DrNe
Update
protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap)
{
foreach (Type entityType in saveMap.Keys)
{
if (entityType.Name == "tblTransactions")
{
foreach (EntityInfo ei in saveMap[entityType])
{
CalculateTransaction(ei);
}
}
}
return base.BeforeSaveEntities(saveMap);
}
private void CalculateTransaction(EntityInfo entityInfo)
{
tblTransactions transaction = (tblTransactions) entityInfo.Entity;
transaction.C_CompletePrice = 1234567;
...
Using BeforeSaveEntities
results in the same strange behaviour:
Entites on the client gets updatet :)
DB not :(
So before i'll use now @dominictus solution (overriding SaveAll) i'm kindly asking for the purpose of those methods i've used (bool BeforeSaveEntity(...) and BeforeSaveEntities(saveMap)). I've consulted the doc and i've watched bryan noyes brilliant pluralsight course but still my simple mind doesn't get it :)
Upvotes: 0
Views: 624
Reputation: 17863
Did you update the EntityInfo.OriginalValuesMap
as described in the ContextProvider
topic?
Upvotes: 1
Reputation: 721
I do it a bit different. Here is an example where I save a timestamp when object was changed. This is a method in my Context class.
public override int SaveChanges()
{
foreach (
var entry in
this.ChangeTracker.Entries()
.Where((e => (e.State == (EntityState) Breeze.WebApi.EntityState.Added || e.State == (EntityState) Breeze.WebApi.EntityState.Modified))))
{
if (entry.Entity.GetType() == typeof(MyClass))
{
var entity = entry.Entity as MyClass;
if (entity != null) entity.UpdatedDateTime = DateTime.Now;
}
}
return base.SaveChanges();
}
In your case, you could just write: entity.C_CompletePrice = 11111111;
As for method BeforeSaveEntity
I prefer using BeforeSaveEntities
. Check breeze documentation for examples.
Upvotes: 0