Updating EDM via Code or Runtime RRS feed

  • Question

  • Hi,

        I am working on a HR System that uses Entity Framework. The database of the application is so dynamic that I want to add a column , add constraints or even work on temp tables through my application (Note: All these features are working in an older version of my application which uses ADO.Net) Now the question is can entity data model support these kind of features..? If Yes please provide me some useful links..!! Thanx

    Friday, October 28, 2011 10:06 AM


  • Hi Raza,

    Entity Framework has some of the features you want, but only if you design the database as a "model first" approach from the beginning.  What this means is you can create your entities from scratch and then EF will generate the DDL for the entire database, the tables, constraints, keys, etc all at once.

     Since you already have an established database with existing tables, this is not going to be an easy thing to accomodate.  You would have to basically do a "hack" and run edmgen.exe on your database to generate the model and mapping files, and then somehow get the resulting XML merged into your EDMX file.

    Even if you get it working, working with the model first approach wouldn't lend itself well to doing dynamic additions to the database like adding new columns and constraints.  The reason being is that model first will create a whole new database when you make a change, dropping all tables and rebuilding them (but not preserving the data). 

    You would be better off making the changes directly on the database and updating the EF model as needed.

    At design time in the EDMX designer you can do Generate Database from Model also and that will create your database DLL for you.  But again, that's not something that's easily made to be dynamic and be run from your application.

    As far as temp tables, the closest things EF has to temp tables are the ability to create complex types from a stored procedure.  

    But really, the need for temp tables as you've used them in the past aren't necessary anymore once you move to EF.  Using Linq to Entities, you work with your entities directly using lambda expressions and Linq statements so you can just create an intermediate result (which would be like a temp table) and then do further Linq queries on that result set as needed.   


    Tom Overton

    Friday, October 28, 2011 12:51 PM