简体   繁体   English

EntityFramework代码优先Oracle

[英]EntityFramework Code First Oracle

I have the follow context, mapping and class that uses for work with oracle: 我具有用于oracle的以下上下文,映射和类:

public class Context : DbContext
    {
        public string Schema { get; set; }

        public Context(string connectionString)
            : base(connectionString)
        {
        }

        public DbSet<User> UserDbSet { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new UserMapping(Schema));
        }
    }

    public class User
    {
        public decimal Id { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
    }

    public class UserMapping : EntityTypeConfiguration<User>
    {
        public UserMapping(string schema = "dbo")
        {
            HasKey(t => t.Id);
            ToTable(schema + ".TBLUSER");

            Property(t => t.Id).HasColumnName("Id");
            Property(t => t.Name).HasColumnName("NAME");
            Property(t => t.LastName).HasColumnName("LASTNAME");
        }
    }

But when i call the: 但是当我打电话给:

var context = new Context("name=ORACLE")
            {
                Schema = "USERTABLESPACEDEFAULT"
            };

        var users = context.UserDbSet.ToList();

The oracle Privider shows me the follow error: oracle Privider向我显示以下错误:

{"ORA-00904: \\"Extent1\\".\\"Id\\": invalid identifier"} {“ ORA-00904:\\” Extent1 \\“。\\” Id \\“:无效的标识符”}

And thats is why EntityFrameworks try to execute the follow query: 这就是为什么EntityFrameworks尝试执行以下查询的原因:

SELECT 
"Extent1"."Id" AS "Id", 
"Extent1"."NAME" AS "NAME", 
"Extent1"."LASTNAME" AS "LASTNAME"
FROM "USERTABLESPACEDEFAULT"."TBLUSER" "Extent1"

Any .dll its necesary ?? 任何需要的.dll ??

Here is my connectionString: 这是我的connectionString:

<add name="ORACLE" connectionString="DATA SOURCE=localhost:1521/XE;PASSWORD=Isolucion2015*;USER ID=SYSTEM" providerName="Oracle.DataAccess.Client" />

Ty changing this line: 请更改此行:

Property(t => t.Id).HasColumnName("Id");

...to this: ...对此:

Property(t => t.Id).HasColumnName("ID"); // Upper case ID.

By default, Oracle's column names are in upper case. 默认情况下,Oracle的列名使用大写。 And when EF generates the names wrapped in double quotes, you have to make sure you get the casing right. 当EF生成用双引号引起来的名称时,您必须确保正确使用大小写。

If you really want to keep using "Id" , then you either have to find a way to have EF not place the double quotes around Id so that the name check is not case sensitive (I don't know how to do that). 如果您确实想继续使用"Id" ,那么您要么必须找到一种方法让EF不要在Id周围加上双引号,以使名称检查不区分大小写(我不知道该怎么做)。

Or, you have to rename the column in Oracle to be exactly Id . 或者,您必须将Oracle中的列重命名为Id

alter table tbluser rename column id to "Id";

But really, I think you should just change your string to "ID" and be done with it. 但实际上,我认为您应该将字符串更改为"ID"并完成操作。

I was recently faced with the same problem of upper case names and Oracle so there is now Nuget package to take care of that. 最近,我遇到了大写名称和Oracle的相同问题,因此现在有了Nuget软件包来解决。

Including EntityFramework.OracleHelpers and doing oneliner will apply uppercase table, column and foreign key conventions. 包括EntityFramework.OracleHelpers和执行oneliner将应用大写的表,列和外键约定。

  using EntityFramework.OracleHelpers;

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
        this.ApplyAllConventionsIfOracle(modelBuilder);
  }

More info and examples are at GitHub page . 更多信息和示例在GitHub页面上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM