0

Here is the question : I have following entities: alt text

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?

1 Answer 1

1

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.