Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate POCO Object classes and DBContext from Entity Framework 6 Model

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 few hours solve the problem without any success.

If I create a new class library, add EF 6 EntityObject Generator and fill the following template variable:

SourceCsdlPath = @"..\..\DataAccess\Model.edmx",

Get the following error in the error list after building:

Error 2 Running transformation: System.IO.FileNotFoundException: Unable to locate file File name: 'C:\Source\EFsource\POCO....\DataAccess\SZOSZRDBModel.edmx'

Server stack trace: at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(String path) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost.ResolvePath(String path) at Microsoft.VisualStudio.TextTemplating31479401930D2C4820ACF71C66B5389A24A5053726798D9718DB676B3FFA30A3454B3CB1EDE2E1C267D5278B1528860C072E81A0E4647BC23993669604048FCD.GeneratedTextTransformation.ApplyUserSettings(UserSettings userSettings) at Microsoft.VisualStudio.TextTemplating31479401930D2C4820ACF71C66B5389A24A5053726798D9718DB676B3FFA30A3454B3CB1EDE2E1C267D5278B1528860C072E81A0E4647BC23993669604048FCD.GeneratedTextTransformation.TransformText()

The Error message is clear, but i do not know, how to set Model path without full absolute path.

I am not sure, using newest version of entity framework is the best idea...

like image 469
Concware Avatar asked Dec 01 '13 22:12

Concware


1 Answers

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".
like image 90
fremis Avatar answered Oct 05 '22 04:10

fremis