1

每当我尝试运行 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(String providerInvariantName) +1426271
WebMatrix.Data.DbProviderFactoryWrapper.CreateConnection(String connectionString) +64
WebMatrix.Data.<>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 源) +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(String connectionStringName, String userTableName, String userIdColumn, String userNameColumn,布尔自动创建表)+51
c:\Users\danderson\Documents\UnitedPiping\LEMTracking\trunk\Source\Web\Global.asax.cs:36 中的 UPI.Web.MvcApplication.Application_Start()

[HttpException (0x80004005):找不到请求的 .Net Framework 数据提供程序。它可能没有安装。]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9860225
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication. InitSpecial(HttpApplicationState state, 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 Framework 数据提供程序。它可能没有安装。]
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 的说明(排序——确切的代码不起作用,所以我在 MSDN 上查找了该类并使用了他们的示例)之后,我收到了一个新错误。“路径中有非法字符。”

  // 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: Illegal characters in path.]
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, Func2 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() +149
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,字符串 userTableName,字符串 userIdColumn,字符串 userNameColumn,布尔 autoCreateTables)+99
WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection(字符串 connectionStringName,字符串 userTableName,字符串 userIdColumn,字符串 userNameColumn,布尔 autoCreateTables)+100
c:\Users\danderson\Documents\UnitedPiping\LEMTracking\trunk\Source\Web\Global.asax.cs:73 中的 UPI.Web.MvcApplication.Application_Start()

4

3 回答 3

1

您不能将 EntityFramework 连接与 WebSecurity.InitializeDatabaseConnection 一起使用。如果您想拥有相同的连接字符串,您可以使用 EntityConnectionSTring Builder

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

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

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

资源

于 2013-08-12T18:07:33.220 回答
0

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

于 2013-08-12T18:02:49.980 回答
0

显然,Entity 不适用于此,即使使用建议的类 allo_man 也是如此。但是,如果我向使用 System.Data.SqlClient 的 Web.config 添加另一个连接字符串,然后将其与 InitializeDatabaseConnection() 一起使用,它就可以工作

于 2013-08-12T18:38:34.150 回答