Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - How to configure DbContext to work with Oracle ODP.Net and EF CodeFirst?

I'm 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) {}

}

Problem is that when I try to do something like this:

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

I get this inner error:

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

And the table exists.

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As Nick wrote in his answer, the issue is related with the quotes and case of the generated query, but not with the table's names but with schema's name:

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

So the solution is very simple, just to uppercase the user id and the schema name:

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

In general, all must be in uppercase: tables, schema and field's names. But it is better annotate each mapped property with the Column attribute instead of uppercase the property name:

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

Thus the names will be easier to read in both database and classes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...