我很难与EF和Oracle合作。早些时候,我开发了一个使用EF和Oracle的应用程序并运行良好。我在本地计算机(GAC)中安装了ODAC,其中包含Oracle.ManagedDataAccess和Oracle.ManagedDataAccess.EntityFramework。
这次我开发了一个使用EF和Oracle的应用程序,但我只想使用Oracle.ManagedDataAccess和Oracle.ManagedDataAccess.EntityFramework的NuGet包。
这是我的app.config
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
<section name="oracle.manageddataaccess.client"
type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
<provider invariantName="Oracle.ManagedDataAccess.Client"
type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client"/>
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<publisherPolicy apply="no"/>
<assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
<bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.121.2.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<dataSource alias="myDataSource" descriptor="(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = host)(PORT = port))(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl)))"/>
</dataSources>
</version>
</oracle.manageddataaccess.client>
<connectionStrings>
<add name="myConnectionString" providerName="Oracle.ManagedDataAccess.Client"
connectionString="User Id=user_id;Password=password;Data Source=myDataSource"/>
</connectionStrings>
</configuration>
我尝试运行一个简单的查询:
pulic IEnumerable<int> GetIDs()
{
var context = new MyDbContext();
var sql = "SELECT ID FROM SOME_TABLE";
return context.Database.SqlQuery<Payable>(sql.ToString());
}
它抛出异常:
Oracle.ManagedDataAccess.Client.OracleException:ORA-01918:user&#39; dbo&#39; 不存在
然后使用this post我将其添加到我的代码中:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("SCHEMA_NAME");
}
嗯,它在我的开发环境中运行良好。 当我尝试在未安装ODAC的服务器中运行它时,它会抛出此异常。
Oracle.ManagedDataAccess.Client.OracleException:ORA-00955:name is 已经被现有对象使用
嗯,这很奇怪,我真的很困惑。请帮忙。
答案 0 :(得分:1)
问题:您试图在上一次迁移的数据库中创建表。
解决方案:首先从数据库中删除所有内容,然后进行迁移。
此链接中有一个Oracle查询,可帮助您确定它 How to drop all user tables?
答案 1 :(得分:0)
我有Oracle数据库10g,当我通过EF Code首先添加迁移时,Visual Studio想要创建另一个具有相同名称的Sequence,删除先前的Sequence,然后问题解决了。