none
Dynamic Query in Entity Framework RRS feed

  • Question

  • Hi Everyone,

    I'm working on a persistence Layer based on ADO.NET Entity Framework.

    i faced a problem creating a dynamic query (what i mean by dynamic is that every single information will be passed to this methode as parameter), that means the context, the class, conditions, retrieved informations (select)... will all be set at runtime and nothing wrote as static code.

    could anyone help me at that point?

    Thank you

    Monday, April 12, 2010 10:39 AM

Answers

All replies

  • Hi MarweN,

     

    Welcome to MSDN Forums!

     

    It is an interesting question.   Entity Framework 4 has been released with the VS2010 RTM.   In EF4, we have many great improvements and new features.    Code Only is one of them and I think it can help on your scenario.  With it, we can define and create the context and entity classes with only codes.   For detailed information, please see

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

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

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

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

     

    You can also find some sample codes in the projects CSEFCodeOnly and VBEFCodeOnly in All-In-One Code Framework, which is an open source project owned by MSDN Forum support team. 

     

    Or if you want these context and entity classes to be generated dynamically without knowing how the database is, you may consider using the EdmGen2.exe to dynamically create the context, entity classes and EDM metadata.  With the classes and metadata, the other LINQ queries or Entity SQLs can be compiled and executed via .NET CodeDOM.   We also have some sample codes in All-In-One Code Framework, CSCodeDOM and VBCodeDOM.

     

    Hope these are what you want.   If you have any questions, please feel free to let me know.

     

    Have a great 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, April 13, 2010 4:01 AM
    Moderator
  • Hi MarweN,

     

    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 day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Supportin Forum

    If you have any feedback on our support, please contactmsdnmg@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, April 16, 2010 1:31 AM
    Moderator
  • Hi Lingzhi

    Thank you very much for your answer, and thank you for links, it's really helpfull :) I'm already using .NET 4 with all these great concepts :D

    But in fact, my problem is about generating a dynamic query: I need to created a methode called genericFind that build a dynamic query, execute it and return the results as object or a collection of object (i'll copy some of my code to make my question more comprhensive!).                                              the signature must be something like this:

    public ObjectQuery genericFind(string dbEntities, string className, string[] columns, string condition)

    {

           //create the query and then execute it

    }

    i found 2 solution:

    - The first: using Entity SQL against the Model but this solution return dbDataReader (not un object) thing, i don't apreciate.

    - The second: using the methode CreateQuery

    ObjectQuery query = dataEntities.CreateQuery<Object>("SELECT Value P FROM ManualDataBaseEntities.Person AS P");

    this return an object but my problem is that i'm limited to the "dataEntities" (and i need it to be also dynamic :( ) and i can't get object of type Person (only with explicit casting which turn me back to static code :( !!!)

    I need to know if i'm in the right way to solution? Or do i miss something?

    PS: I found in some of the links you gave to me something called "builder". it looks great exept that it need the type to be fixed!!! (in the example Vehicule is fixed in the code and it can't be dynamicly set at Runtime)

    builder.Table[“dbo.Vehicles”]= 
        Map.OfType<Vehicle>(
            v => new { .... }

    Thank you again

    Friday, April 16, 2010 10:05 AM
  • Hi MarweN,

     

    Sorry for the delay!  I missed the email notification.  From your post, I think you are trying to use Entity SQL and Code Only in EF4, right?  

     

    Besides, if you want the generic ObjectQuery<> to be dynamically created, I think we need to use .NET Reflection to create the object during runtime like the sample CSCodeDOM and VBCodeDOM does. 

     

    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, April 22, 2010 6:33 AM
    Moderator
  • Thank you Linghzi for your answer. and yes what you said is right :)

    I think that this would be the right solution for my problem. It will be more complicated but i think i can handel it :)

    Thank you again

    Friday, April 23, 2010 10:30 AM
  • It's my pleasure!  :-)

     

    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, April 26, 2010 1:34 AM
    Moderator