Dynamically access table in Entity Framework Core 2.0
Dynamically access table in Entity Framework Core 2.0
24005-Sep-2023
Updated on 06-Sep-2023
Home / DeveloperSection / Forums / Dynamically access table in Entity Framework Core 2.0
Dynamically access table in Entity Framework Core 2.0
Aryan Kumar
06-Sep-2023Yes, dynamuc access to the table is possible in Entity Framework Core 2.0. You can do this by using the
DynamicSet
method of the DbContext class. TheDynamicSet
method takes a lambda expression as a parameter. The lambda expression will be used to dynamically generate the table name.The following code shows how to use the
DynamicSet
method to dynamically generate the table names:C#
In this example, we first create a DbContext called
MyContext
. Then, we use theDynamicTableName
property to dynamically generate the table name. The lambda expression takes the entity ID as a parameter and returns the table name. Finally, we dynamically access the table and output the entity.There are few things to consider when accessing tables dynamically in Entity Framework Core 2.0: