如何配置的DbContext与Oracle ODP.Net和EF codeFirst工作? [英] How to configure DbContext to work with Oracle ODP.Net and EF CodeFirst?
问题描述
我想甲骨文正在与EF codeFirst工作,并ODP.net。这是我的DbContext类:
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"}
和表中存在。
我在做什么错了?
推荐答案
尼克在他的回答中写道,这个问题与生成的查询报价和案件有关,而与表的名称,但是与模式的名称:
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"
所以,解决的方法很简单,只为大写的用户ID和模式名称:
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.
这篇关于如何配置的DbContext与Oracle ODP.Net和EF codeFirst工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!