Partager via


Wrapped System.IO.FileNotFoundException with Entity Framework POCO and Self-Tracking Entities T4 Templates

Visual Studio 2010 and .NET 4.0 were released on Monday! The Self-Tracking Entities template is included in the box, and the POCO Template we released some time ago in Visual Studio Gallery is compatible with the RTM version.

A few days ago we found a small issue in some of our code generation templates. It is really not a major problem with their functionality but rather a case of an unhelpful exception message being shown in Visual Studio when the source EDMX file is not found. So, I am blogging about it with the hope that people getting this exception will put the information in their favorite search engine and will find here what they need to know.

If you open any of our code generation templates you will see near the top a line that contains the name of the source EDMX file, i.e. something like this:

 string inputFile = @"MyModel.edmx";

This line provides the source metadata that is used to generate the code for both the entity types and the derived ObjectContext. The string value is a relative path from the location of the TT file to the EDMX file, so if you change the location of either, you will normally have to open the TT file and edit the line to compensate for the change, for instance:

 string inputFile = @"..\\Model\\MyModel.edmx";

If you make a typo or for some other reason the template cannot find the EDMX file, you will in general see a System.IO.FileNotFoundException in the Error List pane in Visual Studio:

 Running transformation: System.IO.FileNotFoundException: 
   Unable to locate file File name: 
   'c:\Project\WrongModelName.edmx'
...

Now, the exception message above is the version thrown by the “ADO.NET EntityObject Generator” (which is the default code generation template used by EF), and it is quite helpful actually, because it provides the file name that caused the error.

On the other side, if you are using the “ADO.NET POCO Entity Generator” or the “ADO.NET Self-Tracking Entity Generator”, the exception is going to be wrapped in a reflection exception and therefore you won’t directly get the incorrect file name:

 Running transformation: System.Reflection.TargetInvocationException: 
   Exception has been thrown by the target of an invocation. ---> 
   System.IO.FileNotFoundException: Unable to locate file
...

Something very similar happens when you add the template to your project incorrectly. Our code generation templates have been designed to be added through the “Add Code Generation Item…” Option in the Entity Data Model Designer:

image

When you do it this way, we automatically write the name of your EDMX file inside the TT file. But if you add the template to the project in some other way, for instance, using the standard “Add New Item” option in the Solution Explorer, the name of the EDMX file will not be written in the TT file, and instead a string replacement token will remain:

 string inputFile = @"$edmxInputFile$";

When this happens, again, the exception message you get from the EntityObject generator is quite helpful:

 Running transformation: Please overwrite the replacement token '$edmxInputFile$' 
   with the actual name of the .edmx file you would like to generate from.    

But unfortunately, for the POCO and the Self-Tracking Entities templates you will just get a wrapped System.IO.FileNotFoundException as in the examples above.

In any case, the solution is always the same: open the TT file, and replace the token manually with the name or relative path to the EDMX file. Alternatively, remove the template files from the project and add them again using “Add Code Generation Item…”.

I hope you will find this information helpful.

Diego

Comments

  • Anonymous
    April 26, 2010
    The comment has been removed

  • Anonymous
    December 20, 2010
    Found blog on Google. Thanks for the tip!

  • Anonymous
    January 19, 2011
    Thanks for this info , saved me time!

  • Anonymous
    April 15, 2011
    Thanks for article but my problem is I deleted files completely and error is still there. I tired clean solution, rebuilt everything but it isnt get removed. what should I do ? thanks

  • Anonymous
    May 23, 2011
    Perfect! Many thanks for this post.

  • Anonymous
    August 03, 2011
    Thank you.  You saved me ages of googling/binging.

  • Anonymous
    November 20, 2011
    Thanks Diego - updating the path fixed my problem!!!

  • Anonymous
    February 21, 2012
    Excellent, thanks!

  • Anonymous
    February 28, 2012
    yes this worked for me... thanks alot...

  • Anonymous
    August 03, 2012
    I thought my EF framework was hosed and was about to uninstall VS and re-install.  I didn't excpect the SE to return any results when fed the exception, so applaud you thoroughness. Well done!

  • Anonymous
    November 28, 2012
    But it literately 2 years later and the same bug is still there! Maybe its fixed in the 2012 VS

  • Anonymous
    November 30, 2012
    @Peter Doyle: yes this is fixed in the EF 5 templates for Visual Studio 2012.

  • Anonymous
    February 13, 2013
    Thanks a lot .......it worked for me

  • Anonymous
    August 19, 2013
    what a great explanation , thanks a lot  .

  • Anonymous
    April 23, 2014
    Nice elaboration of an unexpected error

  • Anonymous
    July 23, 2014
    I'm using EF 6.1 in VS 2013 and this is NOT fixed. I used the ADO.NET Entity Data Model to get past this. I want to use DB First with DBContext. Do I have to do anything different now because I chose ADO.NET?