在Web.config中调用连接字符串时出现非法字符异常

时间:2013-01-02 07:35:17

标签: c# .net entity-framework asp.net-mvc-4

我正在使用实体框架创建一个C#MVC4.5项目,并在我的Web.config中设置了以下连接字符串:

<connectionStrings>
    <add name="Entities" 
         connectionString="metadata=res://*/PersistenceModel.csdl|res://*/PersistenceModel.ssdl|res://*/PersistenceModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlexpress;initial catalog=TravPro;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" /> 
</connectionStrings>
然后我按如下方式访问此字符串:

string con = ConfigurationManager.ConnectionStrings["Entities"].ConnectionString;
WebSecurity.InitializeDatabaseConnection(con,
                                         "UserProfile", 
                                         "UserId", 
                                         "UserName", 
                                         true);

这会抛出“路径中的非法字符”异常:

[ArgumentException: Illegal characters in path.]
   System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional) +10631190
   System.IO.Path.Combine(String path1, String path2) +42
   WebMatrix.Data.ConfigurationManagerWrapper.GetConnection(String name, Func`2 getConfigConnection, Func`2 fileExists) +208
   WebMatrix.Data.ConfigurationManagerWrapper.GetConnection(String name) +91
   WebMatrix.Data.Database.OpenNamedConnection(String name, IConfigurationManager configurationManager) +18
   WebMatrix.Data.Database.Open(String name) +61
   WebMatrix.WebData.DatabaseConnectionInfo.Connect() +42
   WebMatrix.WebData.SimpleMembershipProvider.ConnectToDatabase() +25
   WebMatrix.WebData.SimpleMembershipProvider.CreateTablesIfNeeded() +35
   WebMatrix.WebData.WebSecurity.InitializeMembershipProvider(SimpleMembershipProvider simpleMembership, DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean createTables) +73
   WebMatrix.WebData.WebSecurity.InitializeProviders(DatabaseConnectionInfo connect, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) +51
   WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(String connectionStringName, String userTableName, String userIdColumn, String userNameColumn, Boolean autoCreateTables) +51
   TravPro.Web.Filters.SimpleMembershipInitializer..ctor() in c:\Work\trunk\TravPro\TravPro.Web\Filters\InitializeSimpleMembershipAttribute.cs:43

有人可以协助并告诉我哪些字符需要转义或如何解决此异常?

3 个答案:

答案 0 :(得分:3)

您存储的连接字符串用于实体框架 - 它不是“常规”ADO.NET连接字符串。实体框架连接字符串包含常规ADO.NET连接字符串(provider connection string=.....中的位) - 但除此之外还有更多内容。

另一方面,

WebSecurity需要常规ADO.NET连接字符串作为输入 - NOT 实体框架连接字符串。

因此,要解决此问题,您需要将WebSecurity的第二个连接字符串添加到配置文件中的<connectionStrings>部分:

<connectionStrings>
    <add name="Entities" 
         connectionString="metadata=res://*/PersistenceModel.csdl|res://*/PersistenceModel.ssdl|res://*/PersistenceModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlexpress;initial catalog=TravPro;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
    <add name="RegularADONET" 
         connectionString="data source=.\sqlexpress;initial catalog=TravPro;integrated security=True;MultipleActiveResultSets=True;" 
</connectionStrings>

然后使用WebSecurity的第二个“常规”ADO.NET连接字符串:

string con = ConfigurationManager.ConnectionStrings["RegularADONET"].ConnectionString;
WebSecurity.InitializeDatabaseConnection(con, "UserProfile", "UserId", "UserName", true);

答案 1 :(得分:1)

我按照你的方式尝试(@marc_s),但是我仍然有问题,尽管它确实引导我朝着解决方案的方向前进。我将connectionStrings更改为:

<connectionStrings>
    <add name="DefaultConnection"
         connectionString="data source=.\sqlexpress;initial catalog=TravPro;integrated security=True;MultipleActiveResultSets=True;"
         providerName="System.Data.SqlClient" />
    <add name="Entities"
         connectionString="metadata=res://*/PersistenceModel.csdl|res://*/PersistenceModel.ssdl|res://*/PersistenceModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlexpress;initial catalog=TravPro;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"
         providerName="System.Data.EntityClient" />
</connectionStrings>

现在一切似乎都很好吃。我创建了一个新的项目模板,运行它,并看到了连接字符串格式的差异。

以下是我的InitializeSimpleMembershipAttribute.cs:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", 
                                         "UserProfile", 
                                         "UserId", 
                                         "UserName", 
                                         true);

感谢您指点我正确的方向。
非常爱。

答案 2 :(得分:0)

我能看到的唯一问题可能是你的“角色 - 尝试使用单引号