Separate POCO Object classes and DBContext from Entity Framework 6 Model

后端 未结 1 1115
日久生厌
日久生厌 2020-12-24 08:04

I started to use Entity Framework 6.0.1 version. I want to separate the generated DbContext and POCO template classes to different class library from the model. I spent a fe

相关标签:
1条回答
  • 2020-12-24 08:52

    Absolute paths are not required. It looks like your relative path is not correct. I am using EF6.1 and I have the POCO classes in a separate project. This is how I got it to work in VS 2013.

    1. Created a Class Library Project and added an ADO.NET Entity Data Model. This project will contain the DB Context.
    2. Added a new EF6.x DbContext Generator item to the DBContext project.
    3. Created a new Class Library Project. This project will contain the POCO objects.
    4. Moved the [Project Name]Model.tt file from the DbContext project to the POCO project.
    5. Edited the [Project Name]Model.tt file. On line 5, I changed:
      const string inputFile = @"SampleModel.edmx";
      to:
      const string inputFile = @"..\DbContext\SampleModel.edmx";
    6. Added a reference in the DbContext project to the POCO project.

    If you are using VS 2013, you can debug the template to see how your relative path is being resolved.

    1. Add a breakpoint to your .tt file.
    2. Right-click the .tt file in the Solution Explorer and select "Debug T4 Template".
    0 讨论(0)
提交回复
热议问题