【问题标题】:Entity Framework: Illegal characters in path. (Connection String) (MVC3)实体框架:路径中的非法字符。 (连接字符串)(MVC3)
【发布时间】:2026-01-08 13:45:01
【问题描述】:

我就是无法解决这个问题:

我正在尝试在我的 MVC3 项目中使用实体框架。 但每次我打电话给Entities context = new Entities(); 我收到此错误:路径中有非法字符。 我确实知道这与我的连接字符串有关,如下所示:

metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=databasename;Persist Security Info=True;User ID=username;Password=password;MultipleActiveResultSets=True"

我使用 Reflector 来确定资源是否正确嵌入到我的程序集中,答案是肯定的,它们以 Models.Model.csdl、.ssdl 和 .msl 的形式完美嵌入到我的程序集中...

我的模型与我尝试连接的程序集在同一个程序集中,并且没有被引用,这意味着通配符 * 应该可以正常工作。

ConnectionString 和向导生成的完全一样……

[ArgumentException: Illegal characters in path.]
   System.IO.Path.CheckInvalidPathChars(String path) +142
   System.IO.Path.Combine(String path1, String path2) +46
   System.Web.Compilation.DiskBuildResultCache.GetBuildResult(String cacheKey, VirtualPath virtualPath, Int64 hashCode, Boolean ensureIsUpToDate) +41
   System.Web.Compilation.BuildManager.GetBuildResultFromCacheInternal(String cacheKey, Boolean keyFromVPP, VirtualPath virtualPath, Int64 hashCode, Boolean ensureIsUpToDate) +237
   System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath virtualPath, Boolean ensureIsUpToDate) +162
   System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +91
   System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +203
   System.Web.Compilation.BuildManager.GetObjectFactory(String virtualPath, Boolean throwIfNotFound) +180
   System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath) +18
   System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext, List`1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations) +228
   System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations) +492
   System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) +198
   System.Web.Mvc.<>c__DisplayClassc.<FindView>b__b(IViewEngine e) +47
   System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths) +177
   System.Web.Mvc.ViewResult.FindView(ControllerContext context) +144
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +151
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +784900
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371

它甚至没有显示问题所在的 ActionResult...

【问题讨论】:

  • 不幸的是,无效字符可能在用户名或密码中。也许你可以把它们乱贴?或者至少列出其中的字符类型?
  • 好吧,我也想到了,它们实际上只是普通字符。没什么特别的,比如“thisisapassword”... soo [a-zA-Z]

标签: c# asp.net-mvc entity-framework .net-4.0 connection-string


【解决方案1】:

最后,删除 SERVERNAME\INSTANCE 中的反斜杠后,它开始工作。 (向我的 SQL Server 实例添加了一个别名,其中没有反斜杠)。可能也可以通过使用 web.config 从非法字符中删除反斜杠来解决...它是如何出现在该列表中的仍然未知

【讨论】: