locked
EF 4.1 Capabilities RRS feed

  • Question

  • Hello

    Can anyone tell me if this is possible? What I want to do is genarate a model at runtime without defining POCO's. To expand on this what I want to do is Add entities by code; In this scenario, the database and table already exists, and I wish to generate a model at runtime, add entities and mappings to existing tables and then access via some kind of repository.

    (Psuedo)

    EntitySet Person = New EntitySet

    Person.Properties.Add(New EntityProperty("UniqueId", INT));

    Person.Properties.Add(New EntityProperty("Forename", INT));

    Person.Properties.Add(New EntityProperty("Surname", INT));

    DBContext con = new DBContext()

    con.EntitySets.Add(Person);

    con.AddPersonMappingsToTable

    etc.

    I may be asking a bit much of the Ef.

    Thanks in advance

     

    Mark

     

     

    Sunday, March 20, 2011 11:14 PM

Answers

  • I don't think it is possible. EF is hiding the database and allows you to work directly with objects rather than the database. For instance, when you want to get some data from the database it reads the data, instantiates an object (entity) of the corresponding type and sets properties accordingly. So it needs objects.

    CodeFirst part of EF (released as part of EF 4.1) allows you to programmatically define/configure your model so that you can tell the EF what property should be mapped to which column or what are the relationships between two entities. It can create a database for your model if there is not one or use an exisitng database (seems like this is your case). Still the model has to be defined before you start working with your entities. Certainly for a single database you can create many models backed by different types but the types need to be somehow defined.

    If you need to access the data via "kind of repository" I wonder why everything has to be dynamic? Does the repository changes dynamically (from the description it does not seem that the database changes dynamically). If the repository does not change dynamically and is stronlgy typed why not define entities based on repository (or even use the types used by the repository if they already exist) and map them using CodeFirst? If you cannot use the types from the repository you can create an adapter that will "know" how to save to the repository the data you get from the database in form of entities.

    Let me know if this helps or try to describe your scenario in a more detailed way to get a better answer.

    Pawel

     

    • Proposed as answer by Jackie-Sun Monday, March 28, 2011 10:02 AM
    • Marked as answer by Jackie-Sun Wednesday, March 30, 2011 2:37 AM
    Monday, March 21, 2011 8:05 PM
  • Patrice / Pawel

    Thanks very much for your answers. The expando objects are very interesting, I will be looking into these.

    Pawel - your comment "I wonder why everything has to be dynamic?" hits the nail on the head really. I have joined a project recently where an application has been written to accomodate being able to dynamically add tables of a fairly uniform nature to the database at runtime. Ok so the same alarm bells ring to me that are ringing with you. But the principle behind the application is good, the technical design however is horrific. So far it has been implemented with code generated linq to sql model, which doesn't accomodate the changes being made at runtime as you have run a util to update the model(s), the code is very poorly written and after the author realised that it is unsupportable and doesn't actually work... he wheel spun into the distance off to his next job. Anyway I digress.

    The company has  now committed to delivering functionality to dynamically add tables at runtime and we are currently redesigning the system, we have written a designer app which you can create tables, they are added to the database plus any stored procedures via smo. These are registered in a metadata structure in the database and can be read and written to at runtime.

    Being a big fan of the EF I was hoping I could generate a model to represent the dynamic table, but it doesn't look that way.

    Thanks for your help again.

     

    Mark

     

     

    • Proposed as answer by Jackie-Sun Monday, March 28, 2011 10:02 AM
    • Marked as answer by Jackie-Sun Wednesday, March 30, 2011 2:37 AM
    Tuesday, March 22, 2011 11:39 PM

All replies

  • I don't think it is possible. EF is hiding the database and allows you to work directly with objects rather than the database. For instance, when you want to get some data from the database it reads the data, instantiates an object (entity) of the corresponding type and sets properties accordingly. So it needs objects.

    CodeFirst part of EF (released as part of EF 4.1) allows you to programmatically define/configure your model so that you can tell the EF what property should be mapped to which column or what are the relationships between two entities. It can create a database for your model if there is not one or use an exisitng database (seems like this is your case). Still the model has to be defined before you start working with your entities. Certainly for a single database you can create many models backed by different types but the types need to be somehow defined.

    If you need to access the data via "kind of repository" I wonder why everything has to be dynamic? Does the repository changes dynamically (from the description it does not seem that the database changes dynamically). If the repository does not change dynamically and is stronlgy typed why not define entities based on repository (or even use the types used by the repository if they already exist) and map them using CodeFirst? If you cannot use the types from the repository you can create an adapter that will "know" how to save to the repository the data you get from the database in form of entities.

    Let me know if this helps or try to describe your scenario in a more detailed way to get a better answer.

    Pawel

     

    • Proposed as answer by Jackie-Sun Monday, March 28, 2011 10:02 AM
    • Marked as answer by Jackie-Sun Wednesday, March 30, 2011 2:37 AM
    Monday, March 21, 2011 8:05 PM
  • Hi,

    The main purpose of EF is to expose data as strongly typed classes. If we take things the other way round what is then the benefit of using EF ?

    Some more details could help. Is this to implement some extensiblity around well defined entities ? In which case you could wrap this in some kind of meta data structure perhaps...

    Assuming you could use something like http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx with EF, I don't really see what is the benefit. IMO working with dynamic objects will be just a nightmare. Why can't you create good old classes ? Of course I assume you know that you could create this from your existing db using the EF designer...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Tuesday, March 22, 2011 11:43 AM
  • Patrice / Pawel

    Thanks very much for your answers. The expando objects are very interesting, I will be looking into these.

    Pawel - your comment "I wonder why everything has to be dynamic?" hits the nail on the head really. I have joined a project recently where an application has been written to accomodate being able to dynamically add tables of a fairly uniform nature to the database at runtime. Ok so the same alarm bells ring to me that are ringing with you. But the principle behind the application is good, the technical design however is horrific. So far it has been implemented with code generated linq to sql model, which doesn't accomodate the changes being made at runtime as you have run a util to update the model(s), the code is very poorly written and after the author realised that it is unsupportable and doesn't actually work... he wheel spun into the distance off to his next job. Anyway I digress.

    The company has  now committed to delivering functionality to dynamically add tables at runtime and we are currently redesigning the system, we have written a designer app which you can create tables, they are added to the database plus any stored procedures via smo. These are registered in a metadata structure in the database and can be read and written to at runtime.

    Being a big fan of the EF I was hoping I could generate a model to represent the dynamic table, but it doesn't look that way.

    Thanks for your help again.

     

    Mark

     

     

    • Proposed as answer by Jackie-Sun Monday, March 28, 2011 10:02 AM
    • Marked as answer by Jackie-Sun Wednesday, March 30, 2011 2:37 AM
    Tuesday, March 22, 2011 11:39 PM