Monday, October 25, 2010

Troubleshooting Entity framework connection string, Unable to load the specified metadata resource.

I was reading the Book called Professional MVC 2 , following the tutorial to Page 44.

When I run the application, /Details/id, which will invoke the Entity framework to do a DB query. No luck, get an strange error.

Line 37:         /// Initializes a new nerdDinnerEntities object using the connection string found in the 'nerdDinnerEntities' section of the application configuration file.
Line 38:         /// </summary>
Line 39:         public nerdDinnerEntities() : base("name=nerdDinnerEntities", "nerdDinnerEntities")
Line 40:         {
Line 41:             this.ContextOptions.LazyLoadingEnabled = true;

[MetadataException: Unable to load the specified metadata resource.]
   System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +2490148
   System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +170
   System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +8517824
   System.Data.Metadata.Edm.MetadataCache.SplitPaths(String paths) +271
   System.Data.Common.Utils.<>c__DisplayClass2.<Evaluate>b__0() +19
   System.Data.Common.Utils.Result.GetValue() +100
   System.Data.Common.Utils.Memoizer`2.Evaluate(TArg arg) +181
   System.Data.EntityClient.EntityConnection.GetMetadataWorkspace(Boolean initializeAllCollections) +292
   System.Data.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection() +29
   System.Data.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor) +205
   System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName) +27
   LADinner.Models.LaDinnerEntities..ctor() in C:\NerdDinner\Models\LaDinners.Designer.cs:39
   LADinner.Models.DinnerRepository..ctor() in C:\NerdDinner\Models\DinnerRepository.cs:10
   LADinner.Controllers.DinnersController..ctor() in C:\NerdDinner\Controllers\DinnersController.cs:14

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
   System.Activator.CreateInstance(Type type) +6
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +491

[InvalidOperationException: An error occurred when trying to create a controller of type 'LADinner.Controllers.DinnersController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +628
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +204
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +193
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +160
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +80
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +45
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841400
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Then I check the web.config.  the connection string is there.

<connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.;Integrated Security=SSPI;database=aspnetdb;trusted_connection=true" providerName="System.Data.SqlClient"/>
    <add name="NerdDinnerEntities" connectionString="metadata=res://*/NerdDinner.Models.NerdDinners.csdl|res://*/NerdDinner.Models.NerdDinners.ssdl|res://*/NerdDinner.Models.NerdDinners.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=NerdDinner;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/></connectionStrings>

So the runtime will read some resource file embedded in the Assembly. So are the name correct?

I just run a ILDASM NerdDinner.DLL /out:a.il

c:\NerdDinner\bin>ildasm NerdDinner.dll /out:a.il

c:\NerdDinnerr\bin>dir
Volume in drive C is BOOTCAMP
Volume Serial Number is 7CA9-BE94

Directory of c:\NerdDinner\bin

10/25/2010  05:23 PM    <DIR>          .
10/25/2010  05:23 PM    <DIR>          ..
10/25/2010  05:23 PM           173,811 a.il
10/25/2010  05:23 PM               824 a.res
10/25/2010  05:19 PM            31,232 NerdDinner.dll
10/25/2010  05:19 PM            46,592 NerdDinner.pdb
10/25/2010  05:23 PM             2,298 Models.NerdDinners.csdl
10/25/2010  05:23 PM             1,205 Models.NerdDinners.msl
10/25/2010  05:23 PM             2,066 Models.NerdDinners.ssdl
               7 File(s)        258,028 bytes
               2 Dir(s)   5,624,713,216 bytes free

Here, for those resource file, Pay attantion to the filename. this is no Assmebly name as the prefix.
Models.NerdDinners.csdl Instead of NerdDinner.Models.NerdDinners.CSDL which is defined in the web.config

so remove the nerddinner in web.config , that’s the trick to fix the problem

<connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.;Integrated Security=SSPI;database=aspnetdb;trusted_connection=true" providerName="System.Data.SqlClient"/>
    <add name="NerdDinnerEntities" connectionString="metadata=res://*/NerdDinner.Models.NerdDinners.csdl|res://*/NerdDinner.Models.NerdDinners.ssdl|res://*/NerdDinner.Models.NerdDinners.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=NerdDinner;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/></connectionStrings>

No comments:

 
Locations of visitors to this page