Here is the question : I have following entities:
Every time I update or insert a transaction I would like to autodetect the category depending on ReferenceMappingExpression
or DescriptionMapppingExpression
in CategoryMappings
Entity.
I mean, I want to match Transaction.Description
to CategoryMappings.DescriptionMapping
and if it matches get the FkCategoryID
from CategoryMapping
and save the transactions.
It is possible to loop through every transaction in list and categorymapping
list but I don't think its good idea. How would you do this? Any suggestions? Any experience of that?
You can use ObjectStateManager, add a partial OnContextCreated method to your entity context. Add new handler to SavingChanges event of context. Get all added and modified transactions there and do whatever you want inside it. Like this:
public partial class ModelContainer
{
partial void OnContextCreated()
{
this.SavingChanges += new EventHandler(ModelContainer_SavingChanges);
}
void ModelContainer_SavingChanges(object sender, EventArgs e)
{
foreach (var item in this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added))
{
SetTransactionDescription(item);
}
foreach (var item in this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified))
{
SetTransactionDescription(item);
}
}
void SetTransactionDescription(System.Data.Objects.ObjectStateEntry entry)
{
Transaction transaction = entry.Entity as Transaction;
if (transaction != null)
{
// Your code
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.