Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework and Dynamic Schema

I inherited an application that talks to many different client databases.

Most of these tables in the client databases have identical schema - but there are a handful of tables that have extra custom columns that contain tax information (ya - bad idea - I know … I didn't set it up).

These extra columns could be named anything. They are known at runtime as they can be looked up in another table.

I can setup EF to that it will read/write these tables (skipping the dynamic columns) but I really do need this information - as it is tax data.

I think my best route it to have a fixed model with extra properties added that could be filled by these dynamic columns.

How can I get Entity Framework to dynamically read and write these columns without using custom SQL statements on every call?

I can do extra reads and writes to read and write these extra columns separately (using custom sql)… but there must be some way to override EF so that it knows about these extra columns and can handle them correctly.

Any help would be appreciated.

like image 382
user3246894 Avatar asked Nov 07 '22 22:11

user3246894


1 Answers

In a first step, you could interrogate the _INFORMATION_SCHEMA_, or other metadata tables directly, to know if the table you want your context to be on has these columns. Based on that information, you can use a different DbContext (generic would probably work) but create it using MappingConfiguration in which you either ignore the columns if they aren't there, or map them to the POCO class your context desires.

like image 100
Tipx Avatar answered Nov 15 '22 08:11

Tipx