none
Creating Runtime Mapping of the POCO Classes using Entity Framework 4.1 RRS feed

  • Question

  • Hi guys, 

    I've a question for you, I want to create a mechanism to allows to load, query and modify an entities using EF 4.1 but the entity mapping should be resolved in runtime therefore in OnModelBuilder method.

    Then my datacontext not have the properties that mapping the entity (for example DbSet<Person> ) but I want to access to the my entity with a generic property for example:

    myContext.Set(typeof(Person)).Where(x => x.Name = "Bob")

    and not

    myContext.Set<Person>.Where(x => x.Name = "Bob") because the Person entity can be a runtime type.

    Is Possible?

     

    Thanks


    Monday, September 5, 2011 10:37 AM

All replies

  • Hello,

    no it is not possible because if you don't have strongly typed set created with generic Set<> you cannot use Linq - compiler will not allow you to call Where(x => x.Name == "Bob") because Where is extension of strongly typed IQueryable<> and x must be of correct type which have Name property.

    Btw. what is a difference in your code snippets? You still have to know a type in both cases. Also ORM is a tool based on design time knowledge - you are mapping and persisting entities your application must use and your database must persist - you know these entities at design time.

    Best regards,
    Ladislav

    Monday, September 5, 2011 12:25 PM
  • Hi, 

    Thank you for the reply,

    Effectively, in my esample the person type is known, but in my project I don't known the type, I know only the Instance of Type class of the Person object because my entities are configurable.

    Is possibile to use the entity framework as configurable orm without write code but only load the entity in runtime mode, creating entity factory etc...?

    I don't know if I got it across.

     

    Thanks

     

     

    Monday, September 5, 2011 1:11 PM
  • Hello,

    EF is not suitable for scenarios where you believe you will configure your ORM later. The database must exists and manipulating mapping at runtime is extremely difficult (it generally means generating XML mapping files at runtime). Also idea of working with EF without knowing types at design time means writing your application as one big exercise of reflection (I don't know how well EF plays with dynamic).

    Best regards,
    Ladislav

    Tuesday, September 6, 2011 7:14 PM