gpt4 book ai didi

c# - Web.config中调用连接字符串出现非法字符异常

转载 作者:行者123 更新时间:2023-11-30 15:36:05 25 4
gpt4 key购买 nike

我正在使用 Entity Framework 创建一个 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

有人可以帮忙告诉我哪些字符需要转义或如何解决这个异常吗?

最佳答案

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

WebSecurity另一方面,期望将常规 ADO.NET 连接字符串 作为输入 - 不是 Entity Framework 连接字符串。

所以要解决这个问题,您需要为 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>

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

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

关于c# - Web.config中调用连接字符串出现非法字符异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14118409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com