无法在MVC中使用InitializeDatabaseConnection()

时间:2013-08-12 16:44:27

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

每当我尝试运行WebSecurity.InitializeDatabaseConnection()时,它都会失败并显示错误

  

无法找到请求的.Net Framework数据提供程序。它可能不会   安装。

以下是方法调用:

WebSecurity.InitializeDatabaseConnection("DataContext", "Users", "Id", "UserName", autoCreateTables: true);

这是我的数据库连接字符串(我在MVC应用程序中使用并且工作正常):

<add name="DataContext" connectionString="metadata=res://*/Entities.DataModel.csdl|res://*/Entities.DataModel.ssdl|res://*/Entities.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(LocalDB)\v11.0;Initial Catalog=DBName;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

这就是我的:

  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
      <remove invariant="System.Data.SqlClient" />
      <add name="SqlClient Data Provider"
       invariant="System.Data.SqlClient"
       description=".Net Framework Data Provider for SqlServer"
       type="System.Data.SqlClient.SqlClientFactory, System.Data,
     Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </DbProviderFactories>
  </system.data>

这里是回调:

  

[ArgumentException:无法找到请求的.Net Framework数据   供应商。它可能没有安装。]
  System.Data.Common.DbProviderFactories.GetFactory(字符串   providerInvariantName)+1426271
  WebMatrix.Data.DbProviderFactoryWrapper.CreateConnection(字符串   connectionString)+64
  WebMatrix.Data&LT;&GT; C_ DisplayClass15.b _14()   +16 WebMatrix.Data.Database.get_Connection()+19 WebMatrix.Data.Database.EnsureConnectionOpen()+ 12
  WebMatrix.Data.d__0.MoveNext()+66
  System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source)+164
  WebMatrix.Data.Database.QuerySingle(String commandText,Object [] args)   +103 WebMatrix.WebData.DatabaseWrapper.QuerySingle(String commandText,Object []参数)+14
  WebMatrix.WebData.SimpleMembershipProvider.CheckTableExists(了IDatabase   db,String tableName)+57
  WebMatrix.WebData.SimpleMembershipProvider.CreateTablesIfNeeded()+49   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(字符串   connectionStringName,String userTableName,String userIdColumn,   String userNameColumn,Boolean autoCreateTables)+51
  UPI.Web.MvcApplication.Application_Start()in   C:\用户\ danderson \文件\ UnitedPiping \ LEMTracking \树干\源\网络\ Global.asax.cs中:36

     

[HttpException(0x80004005):无法找到请求的.Net   框架数据提供者。它可能没有安装。]
  System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext的   上下文,HttpApplication app)+9860225
  System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的   appContext,HttpContext上下文,MethodInfo []处理程序)+118
  System.Web.HttpApplication.InitSpecial(HttpApplicationState状态,   MethodInfo [] handlers,IntPtr appContext,HttpContext context)+172
  System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr的   appContext,HttpContext context)+336
  System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr的   appContext)+296

     

[HttpException(0x80004005):无法找到请求的.Net   框架数据提供者。它可能没有安装。]
  System.Web.HttpRuntime.FirstRequestInit(HttpContext context)+9874568   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)   +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest)   wr,HttpContext context)+254

修改

按照allo_man的说明(sorta - 确切的代码没有工作,所以我在MSDN上查找课程并使用他们的例子),我得到了一个新的错误。 &#34;路径中的非法字符。&#34;

  // Specify the provider name, server and database. 
  string providerName = "System.Data.SqlClient";
  string serverName = "(LocalDB)\v11.0";
  string databaseName = "aoeu";

  // Initialize the connection string builder for the 
  // underlying provider.
  SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();

  // Set the properties for the data source.
  sqlBuilder.DataSource = serverName;
  sqlBuilder.InitialCatalog = databaseName;
  sqlBuilder.IntegratedSecurity = true;

  // Build the SqlConnection connection string. 
  string providerString = sqlBuilder.ToString();

  // Initialize the EntityConnectionStringBuilder.
  EntityConnectionStringBuilder entityBuilder =
      new EntityConnectionStringBuilder();

  //Set the provider name.
  entityBuilder.Provider = providerName;

  // Set the provider-specific connection string.
  entityBuilder.ProviderConnectionString = providerString;

  // Set the Metadata location.
  entityBuilder.Metadata = @"res://*/Entities.DataModel.csdl|
                    res://*/Entities.DataModel.ssdl|
                    res://*/Entities.DataModel.msl";

  //var builder = new EntityConnectionStringBuilder();
  //string sqlConnString = builder.ConnectionString;
  WebSecurity.InitializeDatabaseConnection(entityBuilder.ConnectionString, "Users", "Id", "UserName", autoCreateTables: true);

跟踪:

  

[ArgumentException:路径中的非法字符。]
  System.IO.Path.CheckInvalidPathChars(String path,Boolean   checkAdditional)+10667574 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()+ 1499   WebMatrix.WebData.SimpleMembershipProvider.ConnectToDatabase()+56
  WebMatrix.WebData.SimpleMembershipProvider.CreateTablesIfNeeded()+67   WebMatrix.WebData.WebSecurity.InitializeMembershipProvider(SimpleMembershipProvider   simpleMembership,DatabaseConnectionInfo connect,String   userTableName,String userIdColumn,String userNameColumn,Boolean   createTables)+169
  WebMatrix.WebData.WebSecurity.InitializeProviders(DatabaseConnectionInfo   connect,String userTableName,String userIdColumn,String   userNameColumn,Boolean autoCreateTables)+99
  WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(字符串   connectionStringName,String userTableName,String userIdColumn,   String userNameColumn,Boolean autoCreateTables)+100
  UPI.Web.MvcApplication.Application_Start()in   C:\用户\ danderson \文件\ UnitedPiping \ LEMTracking \树干\源\网络\ Global.asax.cs中:73

3 个答案:

答案 0 :(得分:1)

您不能将EntityFramework连接与WebSecurity.InitializeDatabaseConnection一起使用。如果要使用相同的连接字符串,可以使用EntityConnectionSTring构建器

var builder = new EntityConnectionStringBuilder(entityConnectionString);
string sqlConnString = builder.ConnectionString;

然后使用该连接字符串初始化SimpleMembershipProvider

WebSecurity.InitializeDatabaseConnection(sqlConnString, "User", "UserId", "UserName", autoCreateTables: true);

Source

答案 1 :(得分:0)

在以前的SQL CE版本中,您必须同时安装32位和64位版本才能工作。 检查您的machine.config文件是否正确注册了SQL CE数据提供程序。

答案 2 :(得分:0)

显然,即使使用allo_man建议的类,实体也不会为此工作。但是,如果我将另一个连接字符串添加到使用System.Data.SqlClient的Web.config,然后将其与InitializeDatabaseConnection()

一起使用,则它可以工作