0

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:

  1. Make EF Model Builder map to the Emitted class generated at runtime.
  2. Use reflection somehow to create an Expression that can be used to pass into the IQueryable extension methods
  3. 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.

Community
  • 1
  • 1
C. Tewalt
  • 2,271
  • 2
  • 30
  • 49
  • *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* - are you sure EF cannot help? it depends on the source of the custom columns, you can use Linq To Entity to join them all, project it, filter, sort, ... BTW if you mean to add custom columns to the result, then an anonymous object would be enough for a read-only view. If you want some kind of editable result, then a new View should be created (and you just need to map the stored proc for the view). – Hopeless Sep 28 '15 at 16:17
  • @Hopeless Say we have a view with ColumnA, ColumnB, ColumnC. I have a POCO entity class that has properties for A, B, and C respectively. This works. But now the Customer adds ColumnD into the view and they want the ability to search filter, etc. My POCO entity class doesn't have a ColumnD property in it (unless I add it in and recompile which I can't do in this case) – C. Tewalt Sep 28 '15 at 16:22
  • so does the `ColumnD` have any relationship in your models? it should have at least one. – Hopeless Sep 28 '15 at 16:24
  • `ColumnD` is just an additional column that we want to display in a web page somewhere else. I need the ability to search, order, and filter on `ColumnD` although I don't know that it's called "ColumnD" because it is a custom column that is added to the view by altering it in the DBMS underneath. so- No relationships – C. Tewalt Sep 28 '15 at 16:27
  • Dynamic SQL that maps to EF entities? – Magnus Sep 28 '15 at 16:31
  • `ColumnD` is used to filter the result of the whole record (including all `ColumnA`, `ColumnB`, ...) so it must have some relationship to at least the current table (before adding `ColumnD`). Otherwise it may be some very complex situation (caused by some bad design). – Hopeless Sep 28 '15 at 16:33

0 Answers0