This is a migrated thread and some comments may be shown as answers.

Partial Class question...for Exists

1 Answer 47 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 30 Jul 2010, 06:13 PM
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

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 04 Aug 2010, 03:22 PM
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
Tags
General Discussions
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Alexander
Telerik team
Share this question
or