Entity Framework 6.x Code Frist For Oracle 实践与注意点

时间:2023-11-22 16:54:56

Entity Framework 6.x Code Frist For Oracle 实践与注意点

开发环境

Visual Studio.net 2015/2017

Oracle 11g/12c 数据库

Oracle Client 可以不安装

Asp.net mvc Project

Nuget 组件

  • EntityFramework 6.1.3
  • Oracle.ManagedDataAccess 12.1.2400
  • Oracle.ManagedDataAccess.EntityFramework 12.1.2400

Entity Framework web.config配置

通过nuget安装以上3个组件后,visual studio.net会自动修改web.config的配置信息,会添加以下内容

<configSections>

<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />

</configSections>

<entityFramework>

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />

<providers>

<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />

<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />

</providers>

</entityFramework>

<system.data>

<DbProviderFactories>

<remove invariant="Oracle.ManagedDataAccess.Client" />

<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />

</DbProviderFactories>

</system.data>

<oracle.manageddataaccess.client>

<version number="*">

<dataSources>

<dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=172.20.70.242)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)))" />

</dataSources>

</version>

</oracle.manageddataaccess.client>

<connectionStrings>

<add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=C##EF1;Password=ef1;Data Source=SampleDataSource" />

</connectionStrings>

有了oracle.manageddataaccess.client就不需要安装Oracle客户端了,descriptpor修改成需要连接的Oracle数据库实例就可以了

创建Model/DbContext

public class Blog

{

public int BlogId { get; set; }

[MaxLength(50)]

public string Url { get; set; }

}

public class MyDbContext: DbContext

{

public MyDbContext() : base("Name=OracleDbContext")

{

Database.SetInitializer<MyDbContext>(null);

}

public DbSet<Blog> Blogs { get; set; }

public DbSet<Post> Posts { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

//注意:Oracle必须定义成用户名/所有者或是称Schema,(SQL默认是dbo)

modelBuilder.HasDefaultSchema("C##EF1");

//注意:Oracle必须把所有mapping的字段名转换成大写,否则无法在pl/sql developer中用sql查询

modelBuilder.Properties().Configure(x => x.HasColumnName(x.ClrPropertyInfo.Name.ToUpper()));

//注意:Oracle必须把所有mapping的数据库表名转换成大写,否则无法在pl/sql developer中用sql查询

modelBuilder.Types().Configure(c => c.ToTable(c.ClrType.Name.ToUpper()));

长度/NVARCHAR2类型,否则系统会自动创建成NCLOG类型

modelBuilder.Properties<string>().Configure(c => c.HasMaxLength(250));

modelBuilder.Properties<string>().Configure(c => c.HasColumnType("NVARCHAR2"));

}

}

protected override void OnModelCreating(DbModelBuilder modelBuilder) 可以设定非常多自定义属性,比如ID/主键/一对多/级联删除/命名规则等,具体可以参考以下内容

Entity Framework Fluent API - Configuring and Mapping Properties and Types

Entity Framework Custom Code First Conventions (EF6 onwards)

创建表/同步更新表结构

执行Code first 命令

  • Run the Enable-Migrations command in Package Manager Console
  • Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created
  • Update-Database -script will apply any pending migrations to the database

注意:Update-Database -script 系统会生成数据库的DDL脚本,这样可以方便做一些调整后再执行更新到数据库

如果需要多个库(开发库/测试库/生产库)同步更新就可以通过修改脚本来实现

推荐nugate组件

PM> Install-Package Z.EntityFramework.Plus.EF6

PM> Install-Package Z.EntityFramework.Plus.EFCore

PM> Install-Package Z.ExtensionMethods

都是非常好用的扩展类库