This answer describes a way at runtime to "add" properties to a class by creating a base class of the class you want to add properties to. https://stackoverflow.com/a/14724876/2437521
Would it then be possible to map this class that was created with reflection to a table or view in Entity Framework (in the OnModelBuilding method most likely)
Before people say "That's a dumb idea" let me tell what I'm trying to accomplish. We have a data provider that's compatible with multiple DBMS platforms and using EF as a single code path for this compatibility. We need the ability to use Entity Framework to map to a view in the database to do things like filtering, etc.
This is easy and EF can do these things without trouble. Here's where the problem is. There's a requirement that we be able to join in custom columns into this view and be able to search, sort, filter etc. on these custom columns.
I immediately thought, drat, EF can't do this so I made the method for querying this view just access the vanilla ADO.NET provider underneath and just wrote almost 1000 lines of code with platform agnostic SQL.
Here are the problems I can think of that need to be solved to get this runtime configuration of Entity Framework to work:
- Make EF Model Builder map to the Emitted class generated at runtime.
- Use reflection somehow to create an Expression that can be used to pass into the IQueryable extension methods
- Assume that EF will work with these Expressions and actually generate the SQL needed.
The EF code is something that exists in our core product and it can't be recompiled once it's deployed, hence why I'm trying to accomplish this all at runtime.