create Metamodel at runtime and then generate edmx file ? RRS feed

  • Question

  • Hi

    I'm working on project which should provide for user the ability to work by next scenarios :

    Using WebSite user should be able to manage his own data model (create, change and delete domain entities its properties and relationship between them).

    My task is - when user presses the save button I should to generate EF model with Fluent API (dll), also add Workflow Activities for persistence like Save, Update, and Delete activities and then create schema in database. So actually it the same work which the developer does by creating a EF application, with one difference - all of this must be done at runtime:

    So in order to implement this requirement I'm have to do (correct me if I'm looking into wrong way):

    1) create EDMX file at run-time from metamodel which user build using UI

    2) generate c# classes and WF activities using T4 templates

    3) compile classes into dll which will allow me use the model dynamically.

    The first call to context will create database.

    My main problem is - I don't know how to build edmx at run-time  using model instead db schema. Actually I can build EDMX file at runtime if DB schema exists but how to do the same without schema?  because I use Model First approach there is no schema in db. But ... the EDMX designer in VS works in the same situation. The EDMX designer provides the ability to create entities and then generates the EDMX file according to the metamodel the user drew on the designer surface. So it means there should be a way (possibility) to create edmx file from metamodel instead from db schema.

    I've wrote a lot of text in order to explain what ecactly I need, and ask help from community. So maybe some one can explain me how to create a metamodel and then generate EXMD from the model or there is other way how to solve this problem. Please point me to the API or article how to create metamodel at runtime and then generate edmx.

    feel free to clarify - maybe my explanation was unclear and requires clarification

    • Edited by Dashkov Saturday, December 8, 2012 1:31 PM
    Saturday, December 8, 2012 1:30 PM

All replies

  • Hi,

    Thank you for your post. We are looking into your question and will respond as soon as possible.


    Cathy Miller

    Thursday, December 13, 2012 3:30 PM
  • Hi Cathy,

    Thank you for response. Actually I already wrote code which allows me generate edmx file. As you know edmx is an xml file which consists from three parts: csdl, ssdl and msl. I generated c# classes from XSD files which I found in VS folder. So, now  using c# classes I'm able to provide the ability for customer to create the models which the client wants. Since the code works I started working on comfortable API in order to provide easy and transparent way for developer for manipulation of these classes. I used this approach because I have no found more better way or whatever description which said - what is a right approach for my task. Also nobody told me how this simple task should be solved using EF classes. So my approach works fine but I hope it is temporally solution and I believe there should be more simple and much more natural way to perform my task. Therefore I think the thread is not closed and I'll be looking forward for your advice.



    Friday, December 14, 2012 11:14 PM
  • Hi Andrey,

    I understand that you want to create an EDM model via code. Also you want to do this at runtime. Now for creating an EDM at runtime you can use the Code First approach. Please refer the following –


    This will help you create a model without the EDMX file that you generally create using the Visual Studio designer.

    Next you want to create the POCO classes and also make changes to the schema at runtime. This is where things get tricky. Normally it is the developer’s or the DBA’s job to create and change the schema. If you allow the users to modify the database schema, where do you draw the line? If they can add fields they may also go off changing the definition of existing fields, add or remove indexes etc. This can lead to a support nightmare with bugs, performance issues etc triggered by schema modifications done by users.

    But still if you want to go ahead with this one option is to use CodeDom ( and maybe some reflection to generate classes at runtime, create objects etc. I must warn you that this is very complex and will require a lot of time.

    FYI : I could not find any other easier way. If the above did not help you or is not feasible, I would recommend you to open a support case with us. Visit this link to see the various support options that are available to better meet your needs:;en-us;offerprophone.  




    Monday, January 14, 2013 6:46 PM