Take the 2-minute tour ×

I am getting the following error on my model when trying to generate code based entities and mappings. Here is the error:

Error 1  Running transformation: System.InvalidOperationException: Sequence contains no elements
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at Microsoft.VisualStudio.TextTemplating5FBD4EDD9EACD746112871E64F81C5D3.GeneratedTextTransformation.TransformText()
1 1  

I assume there is not something in my model it is expecting. How can I troubleshoot this?

share|improve this question
Bob: Did you ever figure out the problem? –  Walter Stabosz Feb 15 '13 at 21:56
Did a little hacking of the template, and fixed it for myself, YMMV. –  Walter Stabosz Feb 15 '13 at 22:15
add comment

1 Answer

up vote 1 down vote accepted

The error occurs because a LINQ query in the template uses .Single() which throws an exception when the query returns no results. If you switch the .Single() to .SingleOrDefault() it will not throw the exception.

The template contains multiple calls to .Single(), but through trial-and-error, I found the culprit .Single() (in my case) to be on line 124:

NavigationProperty navPropertyBackReference = navProperty.ToEndMember.GetEntityType().NavigationProperties
    .Where(npBack => npBack.RelationshipType == navProperty.RelationshipType && npBack != navProperty)

Your culprit .Single() may be different. I don't understand this template language at all, so I don't know what the consequences are of allowing the code to continue with an empty sequence. It will probably just cause your output to be missing some data which you'll have to fix manually, but at least this way the exception does not occur and you get all of your files.

It's possible that you can replace all the .Single()s with .SingleOrDefault()s without incident, but to be safe, I only replaced the one that caused my exception.

share|improve this answer
add comment

Your Answer


