ryudice
ryudice

Reputation: 37466

Setting property of objects in datacontext

all my entities have a common property which specifies the company they belong to, I would like to set this property in a method in my datacontext but I do not want to use the partial methods for each of the entity types that the datacontext provides, is there a method that receives any entity that is inserted throught the datacontext so that I can hook to it or override it and set the property using reflection? thanks.

Upvotes: 0

Views: 129

Answers (1)

Oleks
Oleks

Reputation: 32343

You could override the SubmitChanges method in your DataContext class and then perform necessary changes to your inserted/updated/deleted entities:

public partial class YourDataContext
{
    public override void SubmitChanges(ConflictMode failureMode)
    {
        ChangeSet changes = GetChangeSet();

        foreach (var entity in changes.Inserts())
        {
        }

        // you could do the same with updates and deletes

        base.SubmitChanges(failureMode);
    }
}

Upvotes: 1

Related Questions