Separate POCO Object classes and DBContext from Entity Framework 6 Model
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.
- Created a Class Library Project and added an
ADO.NET Entity Data Model
. This project will contain theDB Context
. - Added a new
EF6.x DbContext Generator
item to theDBContext
project. - Created a new Class Library Project. This project will contain the
POCO
objects. - Moved the
[Project Name]Model.tt
file from theDbContext
project to thePOCO
project. - Edited the
[Project Name]Model.tt
file. On line 5, I changed:
const string inputFile = @"SampleModel.edmx";
to:
const string inputFile = @"..\DbContext\SampleModel.edmx";
- Added a reference in the
DbContext
project to thePOCO
project.
If you are using VS 2013, you can debug the template to see how your relative path is being resolved.
- Add a breakpoint to your
.tt
file. - Right-click the
.tt
file in the Solution Explorer and select "Debug T4 Template".
Related videos on Youtube
Concware
Updated on June 04, 2022Comments
-
Concware almost 2 years
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...
-
Ant P over 10 yearsDid you want
@"\..\..\DataAccess\Model.edmx"
?
-