EntityFramework代码优先Oracle [英] EntityFramework Code First Oracle

查看:279
本文介绍了EntityFramework代码优先Oracle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有用于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");
        }
    }

但是当我致电:

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

        var users = context.UserDbSet.ToList();

oracle Privider向我显示以下错误:

The oracle Privider shows me the follow error:

{"ORA-00904:\" Extent1 \.\" Id \:无效标识符"}

这就是为什么EntityFrameworks尝试执行以下查询的原因:

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"

任何.dll都是必需的??

Any .dll its necesary ??

这是我的connectionString:

Here is my connectionString:

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

推荐答案

我最近也遇到了大写名称和Oracle的相同问题,因此现在有Nuget软件包可以解决这个问题.

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.

包括 EntityFramework.OracleHelpers 并执行oneliner将应用大写的表,列和外部文件关键约定.

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);
  }

更多信息和示例位于 GitHub页面.

More info and examples are at GitHub page.

这篇关于EntityFramework代码优先Oracle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆