Dynamically Discovered Entities RRS feed

  • Question

  • Hi,

    I am looking for some solution for this problem: I have a DbContext-derived context which exposes some entities. I want to be able to dynamically find other entities, not known at compile-time, which are derived from already known entities, and update my table model, if necessary. Please note that what I want is, at most, create other tables, which are connected to already existing tables (table per type mapping).

    Can Migrations help me with this? Has anyone done it?



    Friday, March 16, 2012 2:12 PM

All replies

  • Good luck because this is a big task.  Mapping to me is very confusing.  You can see how complex it is in EF, by viewing all that is generated when you bring in a table.  Is it impossible? No!  But the question I'd have is  "Is it worth the effort" ?  Not for me personally because I just don't have internals of MAPPING understood that well.

    JP Cowboy Coders Unite!

    Friday, March 16, 2012 2:27 PM
  • Hi,

    If the actual problem is that you would like to allow users to add "user defined columns" on your existing entities, I would likely try to treat this as a key/value collection so that the application can handle this without any change. Keep in mind also that even if you could alter the mapping dynamically you would still have to take this into account in your forms etc... so you likely need to be able to enumerate those additional columns anyway...

    Migrations is a developper tool that allows to integrate schema changes as part of your application update. Not sure this is what you are trying to do (seems you want a runtime only change ?).

    Else try to explain the underlying problem (for now your description seems rather how you plan to solve it rather than really the actual problem).

    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, March 16, 2012 4:45 PM
  • Hello, Patrice!

    You didn't understand, perhaps I didn't explain well.

    I DON'T ever want to change the existing tables, only add new ones.

    Does that help?



    Friday, March 16, 2012 7:06 PM
  • I would suggest to explain what is the problem you are trying to solve from a non technical point of view. Here you describe a problem you have when trying to apply some kind of solution to a problem you are trying to solve (so as a guess, it looked like to me the original problem could be to have some kind of "extension table" mechanism to allow end users to add their own columns to existing entities in separate tables).

    Basically it seems to defeat the purpose of EF as the goal is to expose data using strongly typed classes. For now it seems to me you want to add additional entities inheriting from existing entities without having to recompile the application (even not a "plug in" like DLL ? perhaps a plug-in like DLL using code first would fit ?). Also have you seen how you'll consume those additional entities from your application ?

    Generally speaking it's always best to give an idea of the original problem rather than just about the problem you have when you try to apply a particular solution.

    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Monday, March 19, 2012 10:30 AM
  • Hi, Patrice, thanks for you reply!

    Here is the situation, in more detail.

    I have a project Core, with a DbContext and some entities. I am using CreateDatabaseIfNotExists<T> initializer, and the first time I access data, it creates the tables for me. So far, so good.

    Suppose some of my entities are meant to be extended, that is, I design them to allow hypothetical derived types with additional properties.

    I want to be able to dynamically discover types in other assemblies that extend those types from project Core that allow extensibility, perhaps using Managed Extensibility Framework, or something else.

    Since these hypothetical types only add properties to the base ones, they can be represented in my database as new tables, which are connected to the existing ones through foreign keys. This is the Table Per Type inheritance strategy.

    What I want is, on the OnModelCreating method of my DbContext, to look up these new types, and add their corresponding tables to the database, if any is found. I was wondering if Code First Migrations allows this: matching registered entities with existing tables and deciding if new tables need to be added.

    Does this make it clearer?

    Again, thanks.


    Monday, March 19, 2012 12:14 PM