简体   繁体   中英

EntityFramework Code First Oracle

I have the follow context, mapping and class that uses for work with 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:

{"ORA-00904: \\"Extent1\\".\\"Id\\": invalid identifier"}

And thats is why EntityFrameworks try to execute the follow query:

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

Any .dll its necesary ??

Here is my 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. And when EF generates the names wrapped in double quotes, you have to make sure you get the casing right.

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).

Or, you have to rename the column in Oracle to be exactly 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.

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.

Including EntityFramework.OracleHelpers and doing oneliner will apply uppercase table, column and foreign key conventions.

  using EntityFramework.OracleHelpers;

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

More info and examples are at GitHub page .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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