2

I have a database model that can be modify by users at runtime:

  • adding new columns to existing tables
  • adding new tables

I want to use Entity Framework Core to access such model.

I'm able of creating the types for the new tables and fields using reflection but I'm not able of creating the DbSet members inside the DbContext class for these new types as the DbSet needs to know the type at compile time.

Does anyone know if this is something that can be achieved with EF Core? A way of injecting the type to the DbSet member dynamically?

1
  • No, EF is definitely not the right tool for this. Use Dapper. Commented May 19, 2020 at 17:57

1 Answer 1

0

It sounds pretty weird to me that the users are the ones defining the tables and their columns, relationships, etc on runtime. Probably what you actually need is to have a structure of tables to support dynamic data, which is much more manageable, that is, a table that defines the UserModels, another table that defines the properties of those models, etc. That will vary a lot depending on your needs.

You could also consider using some special properties like XML data-type fields as suggested here: Dynamically adding a property to an entity framework object

Sign up to request clarification or add additional context in comments.

3 Comments

The user can define its data model in the application and that results in the creating new fields and new tables at database level. We know the structure of the underlying database model and would like ideally create a dynamic context in EF Core to access it.
Yes, but I'm referring to rethinking the approach to get similar results, like suggested here: codeproject.com/Articles/839640/…
Otherwise, if you still go with the actual tables creation/updates, you could use a Micro ORM like Dapper to query it later, which would be much more flexible. dapper-tutorial.net/dapper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.