GetTable equivalent for ObjectContext

后端 未结 1 1028
感情败类
感情败类 2020-12-21 18:23

I was previously using a DataContext which had a GetTable(type) method to get tables generically. Example:

context.GetTable(myObject.GetType())         


        
相关标签:
1条回答
  • 2020-12-21 19:25

    There is indeed a very simple way to do this, like so:

    public IQueryable GetTable<T>(T entity) where T : class
    {
        return context.CreateObjectSet<T>();
    }
    

    Now if I create a Person object and pass to it the generic method, the variable below 'allPeople' will be an IQueryable of people from my database which you can iterate though.

    Person person = new Person();
    IQueryable allPeople = GetTable(person);
    
    0 讨论(0)
提交回复
热议问题