一、Abp項目的下載以及運行。
1.創建abp項目。進入官網 http://www.aspnetboilerplate.com 下載項目模板。abp項目有兩種架構,一種是單頁面(angularjs),另一種是mvc的。點擊創建按鈕。入下圖:
2.還原nuget項目包。打開目錄,生成解決方案會自動下載nuget所需的依賴包。
3.數據庫的還原(abp模板項目用的是Ef的code first 模式)。解決方案里面的 YBE.Web 設置為啟動項目。 打開程序包管理器控制台,默認項目選中YBE.EntityFramework。輸入update-database 。入下圖:
成功之后打開sqlserver數據庫,發現YBE數據庫已經成功創建。
4.Code first 模式的運用。Abp模板項目默認啟用數據遷移功能。
4.1實體類的添加。 在YBE.Core項目中添加一個User類。代碼如下圖:

1 namespace YBE 2 { 3 public class User 4 { 5 /// <summary> 6 /// 用戶Id 7 /// </summary> 8 public int UserId { set; get; } 9 10 /// <summary> 11 /// 用戶名 12 /// </summary> 13 [MaxLength(100)] 14 public string UserName { set; get; } 15 } 16 }
4.2操作接口的添加。在YBE.EntityFramework項目中的 YBEDbContext 類里面添加 public virtual IDbSet<User> Users { get; set; } 。

1 namespace YBE.EntityFramework 2 { 3 public class YBEDbContext : AbpDbContext 4 { 5 //TODO: Define an IDbSet for each Entity... 6 7 8 public virtual IDbSet<User> Users { get; set; } 9 10 /* NOTE: 11 * Setting "Default" to base class helps us when working migration commands on Package Manager Console. 12 * But it may cause problems when working Migrate.exe of EF. If you will apply migrations on command line, do not 13 * pass connection string name to base classes. ABP works either way. 14 */ 15 public YBEDbContext() 16 : base("Default") 17 { 18 19 } 20 21 /* NOTE: 22 * This constructor is used by ABP to pass connection string defined in YBEDataModule.PreInitialize. 23 * Notice that, actually you will not directly create an instance of YBEDbContext since ABP automatically handles it. 24 */ 25 public YBEDbContext(string nameOrConnectionString) 26 : base(nameOrConnectionString) 27 { 28 29 } 30 } 31 }
4.3 映射類的添加(可省略,因為EF可以根據實體類的特性進行映射)。在YBE.EntityFramework項目中的 YBEDbContext 類里面重載 OnModelCreating 方法,並添加對應的映射類。
4.4 更新遷移記錄。在程序包管理器控制台里面輸入 add-migration createUser .注意程序包管理器控制台的默認項目選中YBE.EntityFramework項目。成功后可以在YBE.EntityFramework項目里面的Migrations文件夾下面找到 201702210821387_createUser.CS 。代碼入下:

1 public partial class createUser : DbMigration 2 { 3 public override void Up() 4 { 5 CreateTable( 6 "dbo.Users", 7 c => new 8 { 9 UserId = c.Int(nullable: false, identity: true), 10 UserName = c.String(maxLength: 100), 11 }) 12 .PrimaryKey(t => t.UserId); 13 14 } 15 16 public override void Down() 17 { 18 DropTable("dbo.Users"); 19 } 20 }
4.5 更新數據庫。在程序包管理器控制台里面輸入 update-database -verbose ,執行成功之后可以在控制台里面看到更新數據庫的腳本。更新成功之后數據庫已經創建了Users表。
在實踐的過程中發現,如果多人開發項目不建議用數據遷移功能。因為多人開發的時候會導致遷移文件錯亂,最后導致遷移功能壞掉。
二、Oracle數據庫EF code first的支持。
1.添加Oracle.ManagedDataAccess以及Oracle.ManagedDataAccess.EntityFramework的nuget包。如下圖:
2.修改web.config配置文件。
2.1 添加 <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" />以及<entityFramework><providers><provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" /></providers></entityFramework>節點。如下圖:
2.2 添加<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>節點。如下圖:
2.3 添加<oracle.manageddataaccess.client><version number="*"><dataSources><dataSource alias="QZDB_local1" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=QZDB))) " /></dataSources></version></oracle.manageddataaccess.client>節點。此節點的作用是配置oracle數據庫別名的。也可以不配置,如果沒有配置該節點,連接字符串會找本機 tnsnames.ora 文件里的實例。如下圖:
2.3 修改鏈接字符串。在<connectionStrings>節點中添加 <add name="Default" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=ttt;Password=123;Data Source=QZDB_local1;Persist Security Info=True;" />
3.添加映射架構關系代碼。在YBEDbContext類里面如下代碼:

1 protected override void OnModelCreating(DbModelBuilder modelBuilder) 2 { 3 4 modelBuilder.HasDefaultSchema("TYUM_FS"); 5 base.OnModelCreating(modelBuilder); 6 }
4.測試。在程序包管理器控制台里面輸入 update-database 。沒報錯說明連接成功。如下圖:
說明:Oracle的EF code first 遷移功能目前不能自己創建數據庫。
參考鏈接:http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/dotnet/CodeFirst/index.html#overview