将 Oracle 数据库用于默认的 ASP.NET MVC 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8380924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Oracle database for the default ASP.NET MVC application
提问by devnull
I am trying to make the default ASP.NET MVC application use a remote Oracle database. I have set up the oracle client and can access the remote database from Server Explorer. I tried changing the connection string from
我正在尝试使默认的 ASP.NET MVC 应用程序使用远程 Oracle 数据库。我已经设置了 oracle 客户端并且可以从服务器资源管理器访问远程数据库。我尝试将连接字符串从
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
to
到
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=orcl;Persist Security Info=True;User ID=username;Password=mypassword;Unicode=True"
providerName="Oracle.DataAccess.Client" />
</connectionStrings>
I used the connection string from Server Explorer > Data Connection > orcl.instance
我使用了 Server Explorer > Data Connection > orcl.instance 中的连接字符串
When creating a new user, I am getting the following error in AccountModels.cs
创建新用户时,我在 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
Any idea what am I missing here?
知道我在这里缺少什么吗?
EDIT:
Looks like the following snippet in AccountModels.cs
uses System.Data.SqlClient.SqlConnection
. How can I use the ODP version.
编辑:看起来像下面的代码片段AccountModels.cs
uses System.Data.SqlClient.SqlConnection
。如何使用 ODP 版本。
public class AccountMembershipService : IMembershipService
{
private readonly MembershipProvider _provider;
...
}
采纳答案by adatapost
You have to use ODP.NETAPI instead of SqlClient
provider. The System.Data.SqlClient
classes are for Microsoft SQL server database only.
您必须使用ODP.NETAPI 而不是SqlClient
提供程序。这些System.Data.SqlClient
类仅适用于 Microsoft SQL 服务器数据库。
EDIT:
编辑:
Oracle article : Oracle Providers for ASP.NET Installation
Oracle 文章:用于 ASP.NET 安装的 Oracle Providers
回答by akirti
I've used DevArt Oracle Connector I modified my web.config like this
我使用过 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>
This worked for me. After this too I got issue in ProviderService Project was unable to find it. so I modified this code in IdentityModels.cs
这对我有用。在此之后,我在 ProviderService 项目中也遇到了无法找到它的问题。所以我在 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());
}
}
}
hope it will work for you too.
希望它也对你有用。
回答by Sandaru
回答by Abdullah
1- First you have to install ODAC from the following link select the correct version for the oracle database you are usung . https://www.oracle.com/database/technologies/odac-downloads.html
1- 首先,您必须从以下链接安装 ODAC,为您使用的 oracle 数据库选择正确的版本。 https://www.oracle.com/database/technologies/odac-downloads.html
2- After you Install ODAC on the PC for developing your application , open visual studio and create new project ASP.NET MVC.
2- 在 PC 上安装 ODAC 以开发应用程序后,打开 Visual Studio 并创建新项目 ASP.NET MVC。
3- click server explorer and add connection , from there click change from first line and select oracle database and down from dataprovider list item select ODP.NET , managed driver , if you cannot find this option ODP.NET then you didnt install ODAC correctly reinstall it again untill you can see this option then click continue
3-单击服务器资源管理器并添加连接,从第一行单击更改并选择oracle数据库,然后从数据提供程序列表项中选择ODP.NET,管理驱动程序,如果找不到此选项ODP.NET,则说明您没有正确安装ODAC重新安装再次,直到您可以看到此选项,然后单击继续
4- in the connect window type the user name and password for oracle user and select tns , then select tnsnames.ora file path .
4- 在连接窗口中输入 oracle 用户的用户名和密码并选择 tns ,然后选择 tnsnames.ora 文件路径。
5- Finally click Test Connection.
5- 最后单击测试连接。
6- another way to connect change connection method from TNS to EZ CONNECT and type the user name password database service name database source name
6-另一种连接方式将连接方式从TNS改为EZ CONNECT并输入用户名密码数据库服务名数据库源名
then test connection.
然后测试连接。
I hope this will help :)
我希望这个能帮上忙 :)