Dynamic(Unknown types) Database Operations with EF CORE

Fahri Kaan Göktuna
turkcell
Published in
3 min readJan 31, 2021

--

TL;DR

Nowadays the uptrend of low-code or codeless platforms are fairly remarkable. With the low-code platform that we have developed for our company, we have executed database operations over a metadata file. Please find the methods that you can use in order to connect EF Core or other ORM metadata types also how you can operate those database operations in the seek of the same benefits, at the content of the article.

Introduction

Normally during the time you develop your application, you mostly create your entity classes or types at your coding for your database objects and so model your EF Core by the help of fluent api or by data annotations but at a codeless structure you only have the metadata information of those entity classes and types. As an example; suppose that we have a table named “Customer” that has ID(guid) and Name(String) fields. In order to operate database operations via EF Core, firstly you should create Customer class with its properties and use annotation/fluent api to define the properties and index information for customer entity table or just the opposite at first entity can be created and than by the help of code, you can work migration and so create your table. So how could we do the same without entity class or dynamically coding?

1) Reflection Emit

Reflection Emit is a standard .net library that has been shared by Microsoft, that can support you to create a class, property etc. In this article I’ll be sharing the method to create runtime and their related types via metadata json by TypeBuilder, with the entity, property and navigation definitions on metadata json and the method to create the properties and fields of the entity by the help of IL.

Please find the Type builder factory class in the details of code section below.

2) Introducing the types to EF Core

Now it is time to define the types and properties that have been created by Reflection Emit. In the details of below shown code section, the storage of the types that has been created by runtime over “Dbcontext” and at the modelcreating part definition by fluent api to EFCore has been shared.

3) EF Core ModelCacheKey Factory

At EF Core since your own models are cached by default, the given types to be triggered only once in onmodelcreating method. So, how can we manage runtime new updates at metadata json?

At this point we will be using IModelCacheKeyFactory Interface of EF Core. Please find the details of the execution of cachekeymodel by EF Core and by a version information at metadatajson at the code section shown belown.

Here we should be able to notify the update at metadata information and to reflect the change to dbcontext.

4) Dynamic Type Access on Ef Core DBSet

Everything was so far good till now, so how can we use the generic EFCore DBSet<T> method with the our types?

Again Reflection is required and as the shown code section below, we can use the given type “DbSet” as “IQueryable” by the help of reflection.

5)How to execute Select operations?

We used the given type “DbSet” as “IQueryable” but we do not have the “T” type, so how can we dynamically execute an operation over dbset?

At this point we can use dynamic linq library or building LamdaExpressions method by reflection.

Below you can find the method of building lamda expressions for a specific type or the method to execute operations over IQueryable objects by using dynamix linq.

Using Dynamic Linq:

Using Dynamic “LamdaExpression” Builder:

6) How to perform Add, Update, Delete, operations?

Likely by using the “activator” class we create instances for the types that we have created via reflection. Than filling the properties and fields for those instances in order to call add, update, delete functions over db context to be adequate.

Conclusion

To sum up, in this article, performing details for database operations by using metadata information during codeless or low code platform creation and also the methods to realize that over ORM has been shared. Hope that you enjoy reading the article and hope you’ll find it beneficial for your developments.

--

--