none
Create EDM during runtime?

    Question

  • Hello,

    I have a general question about the ADO.NET entity framework and if it is possible to create a EDM during runtime.

    I'm developing a demo app using WCF RIA Services in VS2010, and using the EF to generate a model of the database. One requirement of my app is that a user has the ability to create a new database table via a UI template they create. This will then generate a new database table or modify columns in an existing table.

    In this case how would I update my EDM to reflect these changes during runtime, or is it not possible to use EF, or RIA services in this context?

    Thanks in advance for any help you may have.

    Victor
    Wednesday, January 20, 2010 2:39 AM

All replies

  • Hello Victor,

     

    Welcome to ADO.NET Entity Framework and LINQ to Entities forum!

     

    Since you are using VS2010, I recommend you consider one of the new features of EF4, Code Only, to generate the EDM metadata with only codes.  For detailed information and sample codes about Code Only, please see:

     

    Code Only (Entity Framework Design blog)

    http://blogs.msdn.com/efdesign/archive/2009/06/10/code-only.aspx

     

    Code Only Enhancements (Entity Framework Design blog)

    http://blogs.msdn.com/efdesign/archive/2009/08/03/code-only-enhancements.aspx

     

    Code Only Further Enhancements (Entity Framework Design blog)

    http://blogs.msdn.com/efdesign/archive/2009/10/12/code-only-further-enhancements.aspx

     

    Feature CTP Walkthrough: Code Only for the Entity Framework (Updated)

    http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx

     

    With this feature, we can use POCO classes to generate the EDM metadata during runtime.  However, if your request is to create the EDM dynamically, it could be more complicated because we need to dynamically create the POCO classes and the process to generate EDM metadata.   If the database is created according to some limited options, it can be easier since we can design several processes to generate the EDM metadata via Code Only.  

     

    I am not an expert of RIA services.  But at the client side, I think we also need the POCO classes to get the data.  For detail, I suggest you consult at the Silverlight forum, WCF RIA Services.   Also, for EF4 issues, we can consult at the pre-release EF forum here, http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads/.   Many product team members are helping us there.  

     

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 20, 2010 6:00 AM
    Moderator
  • Hi Victor,

     

    I am writing to check the status of the issue on your side.  Would you mind letting me know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, January 22, 2010 1:27 AM
    Moderator
  • After reading all the material in the links you provided, thank you, I'm not really sure if it is possible or not.

    Specifically, on one hand it seems you can dynamically create an EDM using the code first approach. But the database needs to be recreated and this will lose all the data in it. This is not acceptable.

    I need to dynamically, during runtime, take a user generated template, create and/ or modify database tables based on the new template while preserving data already in the database. If possible I want to use the Entity Framework model with WCF RIA Services as the data layer since only a couple of database tables are modifiable by the user. The rest of the schema stays constant.

    So my question remains unanswered: Can I generate an EDM dynamically during runtime while preserving data in the database.

    Okay so I changed my original question a bit, but preserving the data was assumed in my original question.

    Thanks for any help you can give to this matter.

    Victor
    Friday, January 22, 2010 6:49 PM
  • Hello Victor,

     

    Sorry if my first post does not make it clear enough.   First, the Code Only feature can help us generate the EDM metadata without pre-defining the Entity Data Model.  Also, we need to know the data structures clearly if we want to use Code Only.  We cannot dynamically create everything, at least we need first declare the POCO entity classes to represent each data tables and create their data mapping and relationships.   Based on your description, some data tables can be modified, so we need to prepare every possibilities of these POCO entities according to the user’s input and then customize the logic of the Code Only to create the ObjectContext.   I think we can use Code Only to dynamically create the EDM metadata, but it is so complicated in my opinion. 

     

    Besides, if the database exists, we don’t need to delete the original one if its data structure meets what we defined in by Code Only.  

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, January 25, 2010 9:19 AM
    Moderator
  • Thanks for your response, Lingzhi.

    Let me see if I understand your comment:
    "Besides, if the database exists, we don’t need to delete the original one if its data structure meets what we defined in by Code Only."

    Are you telling me that I can change the EDM, for example add a column to an existing database table that is already populated with data; without deleting the table or database?

    Thanks again,
    Victor
    Monday, January 25, 2010 6:32 PM
  • Hello Victor,

     

    No, if we want to modify the existing database by the generated EDM, we have to delete the original one and create a new database by ObjectContext.CreateDatabase API.   What I mean is that if the existing database meets what we defined by Code Only, we don’t need first delete the database and then create a new one like the sample demonstrates. 

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, January 26, 2010 2:52 AM
    Moderator
  • Hello Victor,

     

    How is the problem?   If you need any further assistance, please feel free to let me know.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, January 28, 2010 12:57 AM
    Moderator
  • Lingzhi,

    How can one alter an existing EDM, them, to say "add" a column (scalar context) to an exisiting table (& Entity) at runtime? In all cases< I would be adding columns/attributes/scalars to the EDM/Database-and not removing anything from the structure/schema-at runtime.

    Thanks for your discussion.

    pat

    :)

    Thursday, March 17, 2011 5:51 PM
  • Hi,

    Sorry for the late response.  I got the forum notification on this thread just now.   EF has changed a lot in Code First (formerly named as Code First), so I would recommend you use EF4.1 RC for now.   http://www.microsoft.com/downloads/en/details.aspx?FamilyID=2dc5ddac-5a96-48b2-878d-b9f49d87569a&displaylang=en.

    Also, could you please open a new thread for the new question here?   Let me know the thread URL please.

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Wednesday, March 23, 2011 8:11 AM
    Moderator
  • Hi Victor,

    I have the same scenario.

    Did you find any solution?

    Please let me know.

    Saturday, April 21, 2012 11:14 PM
  • On 4/21/2012 7:14 PM, ano0sh wrote:
    > Hi Victor,
    >
    > I have the same scenario.
    >
    > Did you find any solution?
    >
    > Please let me know.
    >
     
    You can't create and EDMX at runtime.
     
    Sunday, April 22, 2012 12:04 AM
  • Hi darnold924,

    Are you sure?


    -- Hamid Eghbalz Technical Manager Douran Co

    Friday, April 27, 2012 2:51 PM
  • Hi,

    What would be the benefit of doing this anyway ? The idea is to create a strongly typed model at compile time so that you can use this strongly typed model in your code. If you create an EDMX at runtime it defeats the purpose.

    As in some cases, knowing what is your goal in doing so could help. For example a case I can think of is to alllow users to somewhat extend the base schema but in this case I would likely have some ad hoc metadata structures being able to handle this even perhaps without any database structural change.

    Note also that the UI, and perhaps some other mechanisms will have to take this into account (for example the user will have to be able to edit data for those additional properties).


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

    • Proposed as answer by ano0sh Friday, April 27, 2012 4:23 PM
    Friday, April 27, 2012 4:10 PM
  • thank you for the answer.

    My problem is a dynamic form generator that needs create tables, entities and other layer objects.

    these dynamic objects have relation to some static objects.

    for example, in an e-shop, the admin can create new products with different properties. books have volume property and Candies have flavor property.

    but when you buy a product, it's Id, price and so on, inserts into selling table.

    what solution can solve this problem?

    any forum? any help?


    -- Technical Manager

    Friday, April 27, 2012 4:18 PM
  • Is this a web application ? If yes, see ASP.NET Dynamic Data else I would model this a entities with a list of admin defined properties...


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

    Friday, April 27, 2012 4:56 PM
  • Yes, it's an ASP.Net web application.

    would you mind suggest some sources?

    Is there any famous opensource?


    -- Technical Manager

    Friday, April 27, 2012 4:57 PM
  • On 4/27/2012 12:18 PM, ano0sh wrote:
    > thank you for the answer.
    >
    > My problem is a dynamic form generator that needs create tables,
    > entities and other layer objects.
    >
    > these dynamic objects have relation to some static objects.
    >
    > for example, in an e-shop, the admin can create new products with
    > different properties. books have volume property and Candies have flavor
    > property.
    >
    > but when you buy a product, it's Id, price and so on, inserts into
    > selling table.
    >
    > what solution can solve this problem?
    >
    > any forum? any help?
     
    Well, you have to write a tool that's going to do this for you. It
    builds the objects, makes the associations and builds a database based
    on some kind of dynamic object and database table generation.
     
    I have seen them, but they are in-house written and not EF based. You
    might be able to find some 3rd party tool.
     
    What you are talking about is the dynamic building of the model. You
    might be able to do an EDMX, as an EDMX is no more than XML that
    represents objects on the model that points to tables on a database.
     
     
    To create an empty .edmx file
     
    1.
    Open or create the project for which you want to create an .edmx file.
     
    2.
    Right-click the project name in Solution Explorer, point to Add, and
    then click New Item.
     
    3.
    Select ADO.NET Entity Data Model in the Templates pane.
     
    4.
    Enter the name for the file (<project name>.edmx), and then click Add.
     
    The first page of the Entity Data Model Wizard appears.
     
    5.
    Select Empty Model in the Choose Model Contents dialog box, and then
    click Finish.
     
    The Entity Data Model Wizard does the following:
     
    Adds references to the System.Data, System.Data.Entity, System.Core,
    System.Security, and System.Runtime.Serialization assemblies to the
    project, if they do not already exist.
     
    Generates an .edmx file to encapsulate the storage model, the conceptual
    model, and mappings.
     
     
    It's just some pointers, but unless you find some 3rd party tool, this
    is something you are going to have to develop.
     
    You might also want to look into the EF Code First approach where you
    build the entities and their relationships by hand (the model), which
    you could develop a generator to do that. There might be a 3rd party tool.
     
     
    Friday, April 27, 2012 8:03 PM
  • So you could be interested in ASP.NET Dynamic Data. see : http://msdn.microsoft.com/en-us/library/cc488545.aspx. It creates a UI from a database.

    Note thought that at some point and especially when adding brand new tables to the database you are back at "developping" the app.

    Here it seems more that my personal preference would be to model those additional properties as its own entity (basically each product would have a list of Name/Value properties perhaps depending on the product category and data would be added to those pre created tables rather than added as real columns to the database schema).


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

    Wednesday, May 02, 2012 8:19 AM
  • Hi,

    I am looking for a solution for the below.

    I am using the Code only approach .I have two entities say Employees and AddressInformation.Where as the AddressInformation is associated with Employees. So while exposing the metadata i dont want AddressInformation to be queried /shown explicitly.Instead it should be navigated from the Employees data result only.

    Kindly advice.

    Wednesday, May 02, 2012 2:50 PM