How to dynamically add columns to a EF DBSet at runtime

Bill Stamp 1 Reputation point
2022-03-09T21:31:56.99+00:00

Moving to a multi tenant architecture

Our tenants will have their own database, where most tables are common between tenants

However 3 tables will have the ability for users to add columns of their own

Using EF in .NET6, how can columns be mapped in to the DBSet for a table at runtime, without creating a new class file and compiling it into the app.?

Alternately if a DLL is created at runtime, how can that DLL be loaded dynamically and added to a DBContext?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,757 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,686 Reputation points
    2022-03-10T19:04:51.563+00:00

    the point of EF is to map database query column values to defined POCO objects. while you could use compiler services to create the dbcontext object at runtime, you would need to use reflection to access it properties.

    it is probably easier to to use native adodb/sqlclient, and load the 3 tables into a custom collection, as you will need custom code to access anyway.

    your other alternative, is to use a json, or xml column for the custom fields.

    0 comments No comments