How to use the poco entity generator

2019-04-22 04:53发布

问题:

I'm using VS2010, and I've download the C# POCO Entity Generator and installed it, now I want to use it.

I can't read the toturial 1 and I can't find any other good toturials, so I've had a go myself - I have created a model and then I'm creating new POCO Entity, but I got the bellow error:

Error 1 Running transformation: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: Unable to locate file
   at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(String path)
   at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(String path)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.VisualStudio.TextTemplatingA9DB2432A51EA8D42A615FBEB2ECB4E5.GeneratedTextTransformation.DynamicHost.ResolvePath(String path)
   at Microsoft.VisualStudio.TextTemplatingA9DB2432A51EA8D42A615FBEB2ECB4E5.GeneratedTextTransformation.MetadataLoader.TryCreateEdmItemCollection(String sourcePath, String[] referenceSchemas, EdmItemCollection& edmItemCollection)
   at Microsoft.VisualStudio.TextTemplatingA9DB2432A51EA8D42A615FBEB2ECB4E5.GeneratedTextTransformation.MetadataLoader.CreateEdmItemCollection(String sourcePath, String[] referenceSchemas)
   at Microsoft.VisualStudio.TextTemplatingA9DB2432A51EA8D42A615FBEB2ECB4E5.GeneratedTextTransformation.TransformText()
   at Microsoft.VisualStudio.TextTemplating.TransformationRunner.RunTransformation(TemplateProcessingSession session, String source, ITextTemplatingEngineHost host, String& result)

What is the problem? I think it can't find the model mapping, if this is the case, how do I resolve this?

1: due to internet restriction on my hometown.

回答1:

Check this article for error description and how to deal with it. It is problem of path substitution when creating POCO template from Add item in Visual Studio.



回答2:

This error occurs when the path to the EDMX file is incorrect in the TT file. To resolve it, open the TT file, and replace the path manually with the relative path to the EDMX file.

For example, change this:

string inputFile = @"Db.edmx";

to something like this:

string inputFile = @"..\DataAccess\Db.edmx";


回答3:

as described in entityframeworktutorial, before POCO entities generation, please disable default code generation which generates Context and Entities code in Model1.designer.cs. To disable default code generation, right click on .edmx, select properties and then remove the value of ‘Custom Tool’ property value ‘EntityModelCodeGenerator’. Now right click on designer surface, select "Add Code Generation Item.." and continue as you did before