Dynamically access table in Entity Framework Core 2.0
Dynamically access table in Entity Framework Core 2.0
652
05-Sep-2023
Updated on 06-Sep-2023
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
DynamicSetmethod of the DbContext class. TheDynamicSetmethod 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
DynamicSetmethod to dynamically generate the table names:C#
In this example, we first create a DbContext called
MyContext. Then, we use theDynamicTableNameproperty 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: