Overriding OpenAccess Base methods

Thread is closed for posting
2 posts, 1 answers
  1. Shawn Krivjansky
    Shawn Krivjansky avatar
    88 posts
    Member since:
    Jan 2010

    Posted 17 Jun 2010 Link to this post

    I have just started using the new ORM Visual Designer.  It is pretty awesome.  I have a number of questions, but one that is eating at me at the moment.

    How would one go about adding their own logic (and extending what happens) when say a .SaveChanges occurs (or .Delete, or .Add) to the context??  Just for example...  What if I wanted to update a table of my own in the database when any UPDATE (.SaveChanges) happens...  What would I change...where?

    Is this possible?

    I understand at the field/property level you can do things to overide/extend what happens in the actual persisted generated business object classes, but the methods I am referring to (.SaveChanges, .Delete, .Add) are at the OpenAccessContextBase class which I don't have easy access to.

    Any advice? 
  2. Answer
    Alexander avatar
    727 posts

    Posted 22 Jun 2010 Link to this post

    Hi Shawn Krivjansky,

    I believe the best way is just to derive from the generated context class and extend your custom context the way you would like:
    public class MyEntityContext : NorthwindEntityDiagrams
        new public void SaveChanges()
            //add any custom logic here
    MyEntityContext context = new MyEntityContext();
    context.Add(new Category() { CategoryName = "Food" });

    You can even use the same method names, just make sure you call the relevant base method at the end. There will be no polymorphic behavior since the base context methods are not virtual but this should not be a problem. You can do this for any method that you may need to customize.
    Hope that helps.

    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top