---
title: "Dynamically access table in Entity Framework Core 2.0"  
description: "Dynamically access table in Entity Framework Core 2.0"  
author: "Revati S Misra"  
published: 2023-09-05  
updated: 2023-09-06  
canonical: https://www.mindstick.com/forum/159844/dynamically-access-table-in-entity-framework-core-2-0  
category: "asp.net core"  
tags: ["database table", "asp.net core", ".net core"]  
reading_time: 2 minutes  

---

# Dynamically access table in Entity Framework Core 2.0

Dynamically [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) in [Entity Framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach) [Core 2.0](https://answers.mindstick.com/qa/44322/what-is-new-in-dot-net-core-2-0)

## Replies

### Reply by Aryan Kumar

Yes, dynamuc access to the table is possible in [Entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach) [Framework](https://www.mindstick.com/forum/34615/software-framework-vs-library) Core [2.0](https://answers.mindstick.com/qa/49733/what-is-web-version-web-1-0-2-0-and-3-0). You can do this by using the `DynamicSet` method of the DbContext class. The `DynamicSet` 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#

```plaintext
using System.Linq;
namespace EFCoreDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a DbContext
            using var context = new MyContext();

            // Dynamically generate the table name
            var tableName = context.DynamicTableName(x => x.Id);

            // Dynamically access the table
            var entities = context.DynamicSet<Customer>(tableName);

            // Print the entities
            foreach (var entity in entities)
            {
                Console.WriteLine(entity.Name);
            }
        }
    }
    public class MyContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
```

In this example, we first create a DbContext called `MyContext`. Then, we use the `DynamicTableName` 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:

- The lambda expression must be a valid expression that returns a string.
- Lambda expressions must be deterministic. This means that the same input must always produce the same output.
- Lambda expression must be thread-safe. This means that the expression must be safe to be executed concurrently by multiple threads.


---

Original Source: https://www.mindstick.com/forum/159844/dynamically-access-table-in-entity-framework-core-2-0

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
