1

I'm using EF 4 and I want to create generic method, which will allow me to load data from tables with only two columns - which I am sure exists in every table and entity. My problem is that I don't know how to get list of entities only knowing type of entity.

    Dictionary<long, string> GetList<T>() where T : System.Data.Objects.DataClasses.EntityObject
    {
        Dictionary<long, string> list = new Dictionary<long, string>();

        //var entityList = this.context.GetEntitiesByType(T);
        //
        foreach (var entity in entityList)
        {
            list.Add(typeof(T).GetProperty("Id").GetValue(entity, null),
                     typeof(T).GetProperty("Name").GetValue(entity, null))
        }
        return list;
    }

Is it possible to obtain this list ? As a workaround I could get table name by using method

    dbContext.GetTableName<T>();

and try to execute sql query, but it seems like bad idea. Any thoughts ?

2
  • 1
    var entityList = this.context.Set<T>() Commented Aug 7, 2014 at 14:20
  • this method is unavailable in EF 4 Commented Aug 8, 2014 at 5:17

1 Answer 1

1

You should use CreateObjectSet< T >() which returns a ObjectSet< T >.

Usage:

context.CreateObjectSet<T>()

For more information http://msdn.microsoft.com/en-us/library/dd382944(v=vs.100).aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.