3

我正在使用实体框架创建一个 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

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

4

3 回答 3

3

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

WebSecurity另一方面,需要一个常规的 ADO.NET 连接字符串作为输入——而不是实体框架连接字符串。

所以要解决这个问题,您需要在配置文件中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);
于 2013-01-02T08:46:23.920 回答
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);

感谢您为我指明正确的方向。
非常喜欢。

于 2013-01-02T11:22:46.733 回答
0

我能看到的唯一问题可能是您的 " 字符 - 尝试使用单引号

于 2013-01-02T07:50:17.293 回答