TAGS :Viewed: 8 - Published at: a few seconds ago

[ Getting a table from a string in EF ]

I'm trying to get a table (entity) from the entity framework context and all I have is a string parameter input to my service.

In ADO.NET I'd do something like:

var tableName = "tablePrefix" + inputString;

How do I solve this in EF?

Any help will be much appreciated :)

Answer 1

To get a Dbset dynamically without knowing the type upfront you can use the Set method on the DbContext. So you could do.

var type = Type.GetType("MyType");
var set = myContext.Set(type);

Once you have the set you can then query to find an entity by primary key etc.

As Kirill says though, why do you want to do this?

Cheers Chris