How to configure DbContext to work with Oracle ODP.Net and EF CodeFirst?

I am trying to work with EF CodeFirst under Oracle with ODP.net. This is my DbContext class:

    public class MyCEContext : DbContext {

    public DbSet<Person> Persons { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Entity<Person>().ToTable("PERSONS","myce");

    }

    public MyCEContext() : 
        base(new OracleConnection(
            "Data Source=cebd; User ID=myce; Password=****;"), true) {}

}

The problem is that when I try to do something like this:

MyCEContext context = new MyCEContext();
Person p = context.Persons.Find(1);

I get this internal error:

{"ORA-00942: table or view does not exist"}

And the table exists.

What am I doing wrong?

+5
source share
4 answers

As Nick wrote in his answer, the problem is with quotation marks and the case of the generated query, but not with the table names, but with the schema name:

SELECT * 
FROM "myce"."PERSONS" "Extent1"

Thus, the solution is very simple, just to smooth out the user ID and schema name:

modelBuilder.Entity<Person>().ToTable("PERSONS","MYCE");

, : , . Column :

    [Column("FIRST_NAME")]
    public string FirstName { get; set; }

, , .

+6

, , , EF Oracle , , .

, :

select name from persons;

EF, , SQL:

select "NAME" from "PERSONS";

OnModelCreating:

modelBuilder.Conventions.Remove<ColumnTypeCasingConvention>();

... POCO , .

SQL, DbContext.Persons. sql, ( )

Oracle EF Code First . , , ODAC , .

+3

ToString linq, dbcontext. SQL.

:

  • .
  • "dbo".
+1

If you do not want to display each column of your application as a workaround for the citation problem mentioned by @fcaldera, you can use the "Devart dotConnect for Oracle" provider.

And only the following code is needed:

Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig config =
                Devart.Data.Oracle.Entity.Configuration.OracleEntityProviderConfig.Instance;

            config.Workarounds.DisableQuoting = true;

Now you can map the "MyObject" class to the MYOBJECT table without any problems. And the same goes for columns.

Note. The NuGet version for "dotConnect for Oracle" does not support the Entity Framework. You must download a trial or professional version from the Devart website.

0
source

All Articles