Click here to Skip to main content
13,005,192 members (60,319 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 15 Oct 2010

Determine the Entity Set for a given Entity.

, 19 Oct 2010
Rate this:
Please Sign up or sign in to vote.
I am writing a generic repository and got stuck on the List<t>() method, as I didn't have any entity set name. Normally you can glean this from an entity instance, like in an Update<t>() method, but List<t>() has no starting point. I deduced the following method to determine the Type of an entity set object that contains entities of type T, which is the type parameter of my repository object.

private static string GetEntitySetName(FoodEntities context)
    var entitySetType = context.GetType().GetProperties().Where(
        p =>
        p.PropertyType.IsGenericType && p.PropertyType.Name.StartsWith("ObjectSet") &&
        p.PropertyType.GetGenericArguments()[0].Name == typeof (T).Name).Single();
    return entitySetType.Name;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Brady Kelly
Founder Erisia Web Development
South Africa South Africa
I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET MVC, with SQL Server, with special fondness for MVC and jQuery. I have been in this business for about eighteen years, and am currently trying to master Angular 2 and .NET Core, and somehow find a way to strengthen my creative faculties.
- Follow me on Twitter at @bradykelly

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 20 Oct 2010
Article Copyright 2010 by Brady Kelly
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid