Dynamically access table in Entity Framework Core 2.0
Dynamically access table in Entity Framework Core 2.0
Student
Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
Yes, 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: