Entity Framework 6.x Code Frist For Oracle 實踐與注意點

阿新發表於2016-12-28

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

//注意:如果欄位名是string型別再沒有設定MaxLength/Type屬性的情況下預設給250長度/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 

都是非常好用的擴充套件類庫

 

相關文章