使用Oracle数据库作为默认的ASP.NET MVC应用程序

时间:2011-12-05 04:09:08

标签: asp.net asp.net-mvc oracle oracle11g

我正在尝试使默认的ASP.NET MVC应用程序使用远程Oracle数据库。我已经设置了oracle客户端,可以从Server Explorer访问远程数据库。 我尝试从

更改连接字符串
 <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

<connectionStrings>
    <add name="ApplicationServices"
         connectionString="Data Source=orcl;Persist Security Info=True;User ID=username;Password=mypassword;Unicode=True"
         providerName="Oracle.DataAccess.Client" />
  </connectionStrings>

我使用了Server Explorer中的连接字符串&gt;数据连接&gt; orcl.instance

创建新用户时,我在AccountModels.cs中收到以下错误

An error occurred while attempting to initialize a System.Data.SqlClient.SqlConnection object. The value that was provided for the connection string may be wrong, or it may contain an invalid syntax.
Parameter name: connectionString

知道我在这里缺少什么吗?

编辑: 看起来AccountModels.cs中的以下代码段使用了System.Data.SqlClient.SqlConnection。我该如何使用ODP版本。

 public class AccountMembershipService : IMembershipService
    {
        private readonly MembershipProvider _provider;
        ...
    }

4 个答案:

答案 0 :(得分:4)

您必须使用ODP.NET API而不是SqlClient提供商。 System.Data.SqlClient类仅适用于Microsoft SQL Server数据库。

编辑:

Oracle文章:Oracle Providers for ASP.NET Installation

答案 1 :(得分:0)

我使用了DevArt Oracle Connector我修改了我的web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings >
    <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-oapp-20140623054602.mdf;Initial Catalog=aspnet-oapp-20140623054602;Integrated Security=True"
      providerName="System.Data.SqlClient" />-->
    <add name="DefaultConnection"  connectionString="User Id=demouser;Password=demo123;Server=oracle;Direct=True;Sid=orcl;" providerName="Devart.Data.Oracle" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <roleManager enabled="true" defaultProvider="OracleExtendedRoleProvider">
      <providers>
        <clear />
        <remove name="OracleExtendedRoleProvider" />
        <!--<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />-->
        <add name="OracleExtendedRoleProvider" type="Devart.Data.Oracle.Web.Providers.OracleExtendedRoleProvider, Devart.Data.Oracle.Web,              Version=8.4.181.4, Culture=neutral, PublicKeyToken=09af7300eec23701" connectionStringName="DefaultConnection" />
      </providers>
    </roleManager>
    <profile defaultProvider="OracleExtendedProfileProvider">
      <!-- <properties>
        <add name="friendlyName"/>
      </properties>-->
      <providers>
        <remove name="OracleExtendedProfileProvider" />
        <add name="OracleExtendedProfileProvider" type="Devart.Data.Oracle.Web.Providers.OracleProfileProvider, Devart.Data.Oracle.Web,               Version=8.4.181.4, Culture=neutral, PublicKeyToken=09af7300eec23701" connectionStringName="DefaultConnection" />
      </providers>
    </profile>
    <membership defaultProvider="OracleExtendedMembershipProvider">
      <providers>
        <remove name="OracleExtendedMembershipProvider" />
        <add name="OracleExtendedMembershipProvider" type="Devart.Data.Oracle.Web.Providers.OracleMembershipProvider, Devart.Data.Oracle.Web,               Version=8.4.181.4 Culture=neutral, PublicKeyToken=09af7300eec23701" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" passwordAttemptWindow="10" />
      </providers>
    </membership>
    <sessionState mode="InProc" customProvider="OracleSessionProvider">
      <providers>
        <add name="OracleSessionProvider" type="Devart.Data.Oracle.Web.Providers.OracleSessionStateStore,Devart.Data.Oracle.Web,               Version=8.4.181.4, Culture=neutral, PublicKeyToken=09af7300eec23701" connectionStringName="OracleServices" />
      </providers>
    </sessionState>
    <siteMap defaultProvider="OracleSiteMapProvider">
      <providers>
        <add name="OracleSiteMapProvider" type="Devart.Data.Oracle.Web.Providers.OracleSiteMapProvider, Devart.Data.Oracle.Web,               Version=8.4.181.4, Culture=neutral, PublicKeyToken=09AF7300EEC23701" connectionStringName="DefaultConnection" securityTrimmingEnabled="true" />
      </providers>
    </siteMap>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31BF3856AD364E35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Razor" publicKeyToken="31BF3856AD364E35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework codeConfigurationType="oapp.Models.MyDbConfiguration, oapp">
    <!-- <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0"/>
      </parameters>
    </defaultConnectionFactory>-->
    <providers>
      <provider invariantName="Devart.Data.Oracle" type="Devart.Data.Oracle.Entity.OracleEntityProviderServices, Devart.Data.Oracle.Entity, Version=8.4.181.6, Culture=neutral, PublicKeyToken=09af7300eec23701" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

这对我有用。 在此之后,我在ProviderService Project中遇到问题无法找到它。所以我在IdentityModels.cs中修改了这段代码

using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
using System.Data.Entity.Migrations;

namespace oapp.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {

        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        static ApplicationDbContext()
        {
            var config = Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;
            config.CodeFirstOptions.TruncateLongDefaultNames = true;

            Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);

            modelBuilder
              .Properties()
              .Where(p => p.PropertyType == typeof(string) &&
                          !p.Name.Contains("Id") &&
                          !p.Name.Contains("Provider"))
              .Configure(p => p.HasMaxLength(256));
        }
    }
    internal class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;

            SetSqlGenerator("Devart.Data.Oracle", new Devart.Data.Oracle.Entity.Migrations.OracleEntityMigrationSqlGenerator());
        }
    }
    public class MyDbConfiguration :  DbConfiguration
    {
        public MyDbConfiguration()
        {
           // this.AddDbProviderServices("Devart.Data.Oracle", new Devart.Data.Oracle.Entity.OracleEntityProviderServices());
            this.SetProviderServices("Devart.Data.Oracle", new Devart.Data.Oracle.Entity.OracleEntityProviderServices());

        }

    }

}

希望它也适合你。

答案 2 :(得分:0)

您必须使用“ODP.NET”API而不是“SqlClient”提供程序。您可以在THIS博文中找到更多详细信息。根据流程进行设置后,您无需更改任何连接字符串。

答案 3 :(得分:0)

1-首先,您必须从以下链接安装ODAC,为您使用的oracle数据库选择正确的版本。 https://www.oracle.com/database/technologies/odac-downloads.html

2-在PC上安装ODAC以开发应用程序后,打开Visual Studio并创建新项目ASP.NET MVC。

3-单击服务器资源管理器并添加连接,然后从第一行单击更改并选择oracle数据库,然后从dataprovider列表项中选择 ODP.NET托管驱动程序,如果找不到该选项ODP.NET,则说明您未正确安装ODAC,请重新安装它,直到您看到此选项,然后单击继续

4-在连接窗口中,键入oracle用户的用户名和密码,然后选择tns,然后选择tnsnames.ora文件路径。

5-最后单击“测试连接”。

6-将更改连接方法从TNS连接到EZ CONNECT并键入用户名的另一种方法 密码 数据库服务名称 数据库源名称

然后测试连接。

我希望这会有所帮助:)