ASP.Net MVC WebSecurity.InitializeDatabaseConnection return 错误

ASP.Net MVC WebSecurity.InitializeDatabaseConnection return error

每当此行执行 WebSecurity.InitializeDatabaseConnection 时就会出现错误 "Connection string "Data Source=BBATRIDIP\SQLSERVER2008R2;Initial Catalog=test;User ID=sa,password=test#" was not找到了。"

再次LazyInitializer.EnsureInitializedreturn错误"An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code Additional information: Exception has been thrown by the target of an invocation."

谁能告诉我我错在哪里。 我的 WebSecurity.InitializeDatabaseConnection() 代码看起来像 WebSecurity.InitializeDatabaseConnection(@"Data Source=BBATRIDIP\SQLSERVER2008R2;Initial Catalog=test;User ID=sa,password=test#", "UserProfile", "UserId", "UserName", autoCreateTables: true);

谢谢

这是我的连接字符串在 web.config

中的样子
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=BBATRIDIP\SQLSERVER2008R2;Initial Catalog=test;User ID=sa,password=tintin11#" providerName="System.Data.SqlClient" />
  </connectionStrings>

但仍然出现错误。有什么想法吗?

这个方法的MSDN签名如下:

public static void InitializeDatabaseConnection(
    string connectionStringName,
    string userTableName,
    string userIdColumn,
    string userNameColumn,
    bool autoCreateTables
)

因此,作为第一个参数,您必须提供 app.config 或 web.config 文件中的连接名称,例如"SecurityConnectionString"