none
create dynamic Entity in runtime RRS feed

  • Question

  • hi

    i use of this code for create dynamic Entity :

    const string CONNECTIONSTRING = "Data Source=.;Initial Catalog=Document;User Id=sa;Password=123;";

    EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(); entityBuilder.Provider = "System.Data.SqlClient";

    entityBuilder.ProviderConnectionString = CONNECTIONSTRING;

    entityBuilder.Metadata = @"res://*/Model1.csdl| res://*/Model1.ssdl| res://*/Model1.msl";

    using (EntityConnection conn = new EntityConnection(entityBuilder.ToString()))

    {

    conn.Open(); // i have Error in this line

    }

    when Debuger arrive to conn.Open();

    i meet this error :

    Unable to load the specified metadata resource.

    please help me how do i can create a Entity in run time

    thanks

    Saturday, December 24, 2011 2:49 PM

Answers

  • Hi vahidbakhtiary;

    There is NO technology / code that will at run time look into your database and extract a schema from the database you want to access. But if you need to do something like this you will have to look into the following:

    Extracting the schema of the database model you wish to create from the database.
    Creating C# class for each table into a text file.
    Creating the associations and mapping between your classes.
    Create a common interface to access the data in the database.
    Need to some how create the metadata of the EDM.
    Compile / emit the above text files into runnable code

    The above should be possible using something like Entity Framework Code First but will not be an easy task.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, December 25, 2011 5:32 PM
  • My undestanding is now that you tried the code in your first post without creating a model first.

    It's best to be always explicit about the situation else as you see it's hard to understand if you are new to EF and misuses it or if you really plan to do something unusual...

    See http://msdn.microsoft.com/en-us/library/bb399572.aspx and in particular the "Entity Data Model Wizard" to see how to create a model from your database.This is done during the design - not runtime step - so that you can use this information to code your application.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Wednesday, December 28, 2011 9:02 AM

All replies

  • hi

    i'll Create Entity in run time

    namely i browse a .mdf file and dynamic create a entity

    please help me how to it do

    thanks

    Saturday, December 24, 2011 12:48 PM
  • Hi vahidbakhtiary;

    I have tested your code on my system and the only change I made to it was the connection string to reflect a database in my database and it worked with no issues.

    I am not sure what you mean by this, "i use of this code for create dynamic Entity :", Does this mean that you have a database in your SQL server called Document and that you are wishing to create a model in your application at run time? If so that will not work. The code you have is to assign a connection string to an already existing model in your application and to point it to another server that has an identical Document database.

    The error message you are getting, "Unable to load the specified metadata resource.", means that it has looked in the memory for the metadata with the name you said it is but could not find it. A couple of reasons for this is that the name you assigned to the metadata is mis-spelled or just not exist. Check the spelling to see if it is spelled the same as in the model. Another reason for this may be that you have instructed Visual Studio to not include the metadata in the executable but to create external files and then you would need to tell entityBuilder.Metadata the path location of the files.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, December 24, 2011 3:59 PM
  • thanks for your answer

    you say right.

    i have a database in your SQL server called Document and that i am wishing to create a model in my application at run time

     

    please help me how to it do

    it's urgent for me

    thanks

     

     

    Sunday, December 25, 2011 6:58 AM
  • Hi vahidbakhtiary;

    There is NO technology / code that will at run time look into your database and extract a schema from the database you want to access. But if you need to do something like this you will have to look into the following:

    Extracting the schema of the database model you wish to create from the database.
    Creating C# class for each table into a text file.
    Creating the associations and mapping between your classes.
    Create a common interface to access the data in the database.
    Need to some how create the metadata of the EDM.
    Compile / emit the above text files into runnable code

    The above should be possible using something like Entity Framework Code First but will not be an easy task.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, December 25, 2011 5:32 PM
  • Hi,

    It's kind of weird. The purpose of entity framework is to provide strongly type access to your data as usual classes. If you want to code against a model that is not known at design time, then it's perhaps not the right approach.

    What will you do once your app created a model ?

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Sunday, December 25, 2011 7:17 PM
  • Hi vahidbakhtiary,

    Welcome to MSDN Forum.

    I agree with Patrice,The purpose of entity framework is to provide strongly type access to your data as usual classes. If you have an exist database, I suggest you to use database first.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, December 27, 2011 1:48 PM
    Moderator
  • thanks for your answer if possible show me with code or sample thanks
    Wednesday, December 28, 2011 6:39 AM
  • My undestanding is now that you tried the code in your first post without creating a model first.

    It's best to be always explicit about the situation else as you see it's hard to understand if you are new to EF and misuses it or if you really plan to do something unusual...

    See http://msdn.microsoft.com/en-us/library/bb399572.aspx and in particular the "Entity Data Model Wizard" to see how to create a model from your database.This is done during the design - not runtime step - so that you can use this information to code your application.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Wednesday, December 28, 2011 9:02 AM
  • Hi vahidbakhtiary;

    As I stated before you can not build a model in the way you are asking for a solution. Entity Framework is a strongly type model as is C# a strongly typed language and because of this the model must be known at compile time and there is no tools out there that will do this. In my last post I posted a list of things that may give you what you need but a test project must be built to do a proof of concept. In doing that you will need to research all the steps and code it and test it.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, December 28, 2011 2:03 PM
  • thanks for your answer

    please help me i need to what steps and code

    thanks

    Sunday, January 1, 2012 12:21 PM
  • Hi vahidbakhtiary,

    Fernando has provided you the steps, but it exists in theory. Entity Framework is a strongly type model, it is convenient for you to operate entities, if you create the entity dynamically, you can't benifit by the Entity Framework's advantage.

    Best Regards 


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 6, 2012 2:13 AM
    Moderator
  • Could you rephrase what you need ? Entities are not created at runtime. Instead you create a strongly typed model at design time either by inferring it from the database with some tooling (database first, model fist) or by writing some code (code first). If youi are starting with EF see http://msdn.microsoft.com/en-us/data/ee712907.

    Else it doesn't seems usable (how would you consume a strongly typed model created entirely at runtime ?).

    Or is this how you call the "code first" model ?

     

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Friday, January 6, 2012 9:15 AM
  • Hi,

    My question is factically the same.

    How could I add new attributes in Entity in runtime?

    For example:

    I've 2 predefined fields for Customer Entity: Code, Name.

    User wants to add on the form and enter some additional attrbutes for Customer like Address, PostalCode, SalesAmount and etc.

    How can implement such logic in runtime using EF?


    Programmer

    Wednesday, November 21, 2012 1:45 PM
  • Hi,

    Try http://stackoverflow.com/questions/923778/how-would-you-design-your-database-to-allow-user-defined-schema

    Basically you could have an approach where you model what is needed to handle metadata. For example you could add a table that defines those properties and one (or more) that hold those values. On each entity you could then have a Properties collection that would expose them (done this before EF so that I can handle additional user defined data without having to alter the schema).

    Or you can expose all or part of SQL features (with code generation/compilation). In this case, really take care to split apart those user defined extensions from your own controlled entities to avoid having issues with upgrades etc...

    IMO the first approach interest is to avoid any database change (if you change the db schema, you are basically starting to just reinvent a development process).

    And keep in mind that this not the only issue (i.e. your app will have to take this into account on forms and perhaps reports etc...). Define perhaps first to where you allow them to go (at some point changing too much the application liekly needs to be a development activity process).


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

    Wednesday, November 21, 2012 4:45 PM
  • Hi,

    Is it the same situation with Entity Framework 7.0?

    Is it possible dynamically extend entities now?


    Programmer

    Monday, September 7, 2015 11:52 AM