Edmx codegenerator for legacy project

EunMin Lee 20 Reputation points
2023-02-01T21:25:28.9633333+00:00

I have been assigned a task required to use VS 2012 edmx generator.

Therefore, I have to get a license for VS 2012 although we bought and use VS 2022.

 

I am looking for a way that I do not have to install VS 2012. But the code won’t generate

like VS 2012 does.

 

I wonder if there is a way to use the old version of code generation.

Any help would be appreciated.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,264 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,514 questions
0 comments No comments
{count} votes

Accepted answer
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-02-01T21:33:41.43+00:00

    The EDMX files and Entity Framework 6 Tools are still supported in Visual Studio 2022. You just need to make sure to include the in installation or modify visual studio installation to include them.

    The custom tool for the edmx file is still set to EntityModelCodeGenerator but the main trick is being done with the t4 files (Model1.Context.tt and Model1.tt).

    Example - Create a Database first model VS 2022 EDMX for EF 6.0

    1. Create a Windows Forms Application (.NET Framework 4.8).
    2. Add a new Item, and from the list, choose ADO.NET Entity Data Model, set a name and press enter.
    3. In the Entity Data Model wizard, choose EF Designer from Database, click next.
    4. Choose an existing data connection or create one, and click next.
    5. Choose the tables (and you can decide to include foreign keys or pluralize table names) and click finish.

    There you go, your edmx file will be generated and you can see the C# model classes and the DBContext generated for is with the .tt files.

    Code generation from EDMX for other versions

    You can just copy t4 files that you generated in above example, or T4 templates from older versions to any existing project as well.

    You can use the following templates:

    Or you can search for many other templates available.

    Or see Designer Code Generation Templates which explains about different templates.

    The only thing you need to do is:

    1. Download the VSIX from above links
    2. Change the file extension to *.zip
    3. Open the zip file and find CSharpDbContext.Context.tt and CSharpDbContext.Types.tt.
    4. Or if you have found them somewhere else, just copy them in your project
    5. Assuming you have MySampleDB.edmx, then copy those two .tt files along with the edmx file, and rename them to MySampleDB.Context.tt and MySampleDB.tt.
    6. Then open each file and replace Model1.edmx with MySampleDB.edmx.
    7. Then include those files in the project. They will be added under the MySampleDb.edmx . Just to be on the safe side, set the custom tool of the edmx file (in properties) to EntityModelCodeGenerator and Let the Code Generation Strategy be T4.

    Legacy EF 4.X Object Context Generator

    If you want to use Legacy Object Context Generator:

    1. Make sure you have installed Entity Framework 6 Tools for VS
    2. Create your EDMX file
    3. Deleted the *.tt files from your project which are beside your edmx file.
    4. Open the EDMX designer and right click on an empty space and choose properies.
    5. Right click on Code generation Strategy, and choose Reset
    6. Save the edmx.

    User's image

    For more information see:

    VS Installer - Including Entity Framework 6 Tools

    Open Visual Studio Installer, click modify, and then in Individual components, search for Entity, and check Entity Framework 6 tools.

    User's image


1 additional answer

Sort by: Most helpful
  1. Jack J Jun 24,276 Reputation points Microsoft Vendor
    2023-02-06T08:21:05.5833333+00:00

    @EunMin Lee, Welcome to Microsoft Q&A, based on my search, you could refer to the following link to use DbContext Generator for EF 4.x.

    DbContext Generator for EF 4.x

    Hope my solution could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.