Monday, 4 July 2016

HOW TO USE EDMX FILE AND OBJECT QUERY IN VISUAL 2012 AND LATER

In order to use object query in your program you'll need to include include system.data.entity as a reference in your project. To do that you go to "references" click on "add reference" and you find it and add it, after adding it you'll include in your code "using system.data.objects;". Now in order to use the edmx file with object query object context there are things you need to understand. object context is a form of db querying used in older visual studio versions, the one used in visual 2012 and later is dbcontext. Therefore in order to use object context in visual studio 2012 and later you'll need to revert your entity framework to the format used in older visual studio. To do this... First you'll go to the edmx model, click on an empty space, then go to properties. You'll notice that "code generation strategy" is set to T4 without any option for you to change it. This occurs in entity framework versions 5 and later which you'll find in visual studio 2012 and later. in older versions you are given a choice between "T4" and "Legacy object context"- which doesn't appear in entity framework 5 or later - Now in order to use object context, you'll need to add entity framework 4 to your project. this is done by going to tools - nuget package manager - package manager console. When it opens well, type in "install-Package EntityFramework -Version 4.3.1" or any other version 4 you want. and allow it to install. when it is done, you then go back to code generation strategy, and you'll see that legacy object context has been added. Now when you are done you go to your Project solution folder and find the folder for edmx model. click on its arrow to drop down. Then delete all the ".tt" files because those files are generated only for T4. Therefore once you've changed to legacy object query you delete the .tt files. Now in that edmx folder you can open edmxModel.designer.cs file (depending on the name you gave it). You'll notice that when the code generation strategy was in T4 there was no code there but after changing to legacy object context some codes are automatically generated in it. Now with this done you can now use emdx file with your object query in visual 2012 or later. thanks.

No comments: