Partial Class question...for Exists

Thread is closed for posting
2 posts, 0 answers
  1. Steve
    Steve avatar
    1888 posts
    Member since:
    Dec 2008

    Posted 30 Jul 2010 Link to this post

    I want to extend my partial class to have an extension or a static method for .Exists or something like that so I can know if it's already in the database before trying an insert

    Do I have to make and dispose a new context in the partial or can it leverage anything?....does this functionality already exist somewhere?  I just want to check .Exists on the PK
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 04 Aug 2010 Link to this post

    Hello Steve,

    You can do something like this:
    public partial class MyEntityDiagrams
    {
        public bool Exists<T>(object id) where T: class
        {
            IObjectId objectId = Telerik.OpenAccess.Database.OID.ParseObjectId(typeof(T), id.ToString());
            try
            {
                this.GetScope().GetObjectById<T>(objectId);
            }
            catch (Telerik.OpenAccess.Exceptions.NoSuchObjectException)
            {
                return false;
            }
            return true;
        }
    }
    Of course this partial class should have the same name and namespace as your auto-generated context. Additionally, you should put it in a separate file so the method does not get deleted when the context is regenerated. Then you can just instantiate a context and call  the method.
    Hope that helps.

    Greetings,
    Alexander
    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