none
DbContext in a different project than edmx model RRS feed

  • Question

  • Hello

    I have a solution that is ment to be separated over several projects: Model, Data, DataAcces, Business, etc.

    In my Model project I have a model.edmx made with EF 5.0, database first (so far only two tables: component and category)

    When creating the edmx, VS stores under it the templates for constructing the POCO classes and the dbContext.

    I then tried to separate those 3 components (model, entities and context) over 3 different projects and couldn't do so. I created a project named Data and create a new item EF 5.0 DbContext Generator. I edit the tt template in order to make it targeted the edmx file in my Model project. As you might expected, the entities where properly created.


    However, when trying to get that context created in Data prj to work my aplication crashes in runtime on the last line of the folowing code:

    using (EFContext context = new EFContext())
    {
        var repository = context.Category;
    
        return repository.Count();
    }

    The application throws an exception on the return statement, saying "The entity type Category is not part of the model for the current context.", although it was created based on Model.edmx file.

    I have past several hours trying to get this done with no success... I managed to separate the entities to another project but my context seems not to work outside Model project. When using the context generated default by model.edmx in Model project, changing it to use the entities generated in the Data project, all goes well and it works. Whe I try to create another context in another project I never get it done... my context in Data project, for instance, never works.

    I'm becoming very frustated and my deadline is approaching. I have to separate this in Model (only the edmx), Data (only data entities POCO classes) and DataAccess (only dbContext)

    Please help me here!

    Best regards,

    Jorge


    • Edited by madmicra Sunday, January 19, 2014 8:49 PM
    Sunday, January 19, 2014 8:44 PM

Answers

  • I have found the solution.

    It appears that, as Model.dll is not used in the application assembly, the compiler do not include it when compiling the code.

    In order to get the model as a resource as posted in connection string, you will have to force the compiler to load the assembly Model.dll. If you do not provide this it won't include the model in final solution and then you will be unable to get the EF working has your dbcontext will not get the ssdl, csdl, msl files.

    You only have to include a statment like Assembly.Load("Model"); anywhere in app code before create the context.

    Hope it helps someone in the future! Lost several hours in this...

    Regards

    Jorge

    Sunday, January 19, 2014 10:21 PM

All replies

  • I have found the solution.

    It appears that, as Model.dll is not used in the application assembly, the compiler do not include it when compiling the code.

    In order to get the model as a resource as posted in connection string, you will have to force the compiler to load the assembly Model.dll. If you do not provide this it won't include the model in final solution and then you will be unable to get the EF working has your dbcontext will not get the ssdl, csdl, msl files.

    You only have to include a statment like Assembly.Load("Model"); anywhere in app code before create the context.

    Hope it helps someone in the future! Lost several hours in this...

    Regards

    Jorge

    Sunday, January 19, 2014 10:21 PM
  • Hello,

    Glad to hear that you have found a solution and share it with us.

    Cheers.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 20, 2014 9:59 AM
    Moderator