Alex Rivera | Logout

Create a DbSet<T> dynamically in Entity Framework?

Asked 2011-03-15T06:43:49.387
31

In LINQ to SQL, I can create a repository dynamically using DataContext.GetTable<T>. Is there a similar way to do this in Entity Framework 4 other than declaring the properties on the specific DbContext? For example:

public MyDbContext: DbContext
{
    public DbSet<MySet> MySets {get; set;}
}

I would like to know how can I create/get a reference to MySets dynamically as I can with LINQ to SQL as in:

var mySet = MyDbContext.GetTable<MySet>();
Edit
Report

1 Answer

48

DbContext has method for this:

  var set = context.Set<MyEntity>();
answered 2011-03-15T08:54:07.183

Your Answer