1. Normal & Simple
先介紹一種最簡單的構建方式,人人都會。
新建 Console 應用程序,命名自定
安裝相關Nuget 包
//Sql Server Database ProviderInstall-Package Microsoft.EntityFrameworkCore.SqlServer //提供熟悉的Add-Migration,Update-Database等Powershell命令,不區分關係型數據庫類型Install-Package Microsoft.EntityFrameworkCore.Tools
自定義 DbContext
public class MyContext:DbContext{ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=ConsoleApp;Trusted_Connection=True;MultipleActiveResultSets=true;"); } }
執行遷移和更新命令
Add-Migration InitializeUpdate-Database
使用方式
using (var context = new MyContext()) { // TODO}
剛以上,我們便見識到了了一種最平常也是最簡單的使用方式,接下來,讓我們用其他方式去慢慢地改造它,從而儘可能地接觸更多的用法。
2. Level Up
2.1 準備工作
將第一步生成的數據庫,遷移文件和使用方式內容全部刪除。
2.2 更新 MyContext 內容
刪除 MyContext 中的 OnConfiguring 方法及其內容,增加含有 DbContextOptions 類型參數的構造器,我們的MyContext看起來應該是下面這個樣子。
public class MyContext : DbContext{ public MyContext(DbContextOptions options) : base(options) { } }
假如我們此時仍然再執行遷移命令,VS將提示以下錯誤
No parameterless constructor was found on 'MyContext'. Either add a parameterless constructor to 'MyContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'MyContext'.
添加無參構造器的方式之後再講解,先來按照提示信息添加一個 IDbContextFactory 的實現類。
public class MyContextFactory : IDbContextFactory<MyContext> { public MyContext Create(DbContextFactoryOptions options) { var optionsBuilder = new DbContextOptionsBuilder<MyContext>(); optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=ConsoleApp;Trusted_Connection=True;MultipleActiveResultSets=true;"); return new MyContext(optionsBuilder.Options); } }
之後再次運行遷移和更新數據庫的命令也是水到渠成。
2.3 使用方式:構造器實例化
既然 MyContext 含有 DbContextOptions 類型參數的構造器,那就手動創建一個參數實例注入即可。
var contextOptionsBuilder = new DbContextOptionsBuilder<MyContext>(); contextOptionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=ConsoleApp;Trusted_Connection=True;MultipleActiveResultSets=true;");// 注入配置選項using (var context = new MyContext(contextOptionsBuilder.Options)) { // TODO}
經此,我們知道了遷移命令會檢測 Context 的相關配置入口,只有在滿足存在 OnConfiguring 方法或者存在自建 IDbContextFactory 實現類的情況下,命令才能成功運行。
3. Day Day Up
目前爲止,我們已經知道如何手動遷移和實例化 Context 的步驟了所以讓我們更進一步。寫過 ASP.NET CORE 的人可能知道在 ASP.NET CORE 中,Context 常常以依賴注入的方式引入到我們的 Web 層,Service 層,或者 XXCore 層中(話說筆者最近最喜歡的解決方案開發架構就是僞 DDD 的四層架構,有空再介紹吧)。其實在 Console 應用中,這也可以很容易實現,具體的依賴注入引入可以參考筆者的上一篇博客,所以最終的代碼效果如下:
var serviceCollection = new ServiceCollection(); serviceCollection.AddDbContext<MyContext>(c => { c.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=ConsoleApp;Trusted_Connection=True;MultipleActiveResultSets=true;"); });var serviceProvider = serviceCollection.BuildServiceProvider(); using (var context = serviceProvider.GetService<MyContext>()) { //context.Database.Migrate();}
至此,我們便基本完成了本文的主題,唯一有些美中不足的是我們的數據庫連接字符串好像到處都是,這不是什麼大問題,筆者直接將 Configuration 的配置代碼貼在下面,這也是 ABP 中的方式。
public class AppConfigurations{ private static readonly ConcurrentDictionary<string, IConfigurationRoot> ConfigurationCache; static AppConfigurations() { ConfigurationCache = new ConcurrentDictionary<string, IConfigurationRoot>(); } public static IConfigurationRoot Get(string environmentName = null) { var cacheKey = "#" + environmentName; return ConfigurationCache.GetOrAdd( cacheKey, _ => BuildConfiguration(environmentName) ); } private static IConfigurationRoot BuildConfiguration(string environmentName = null) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, true); if (!string.IsNullOrWhiteSpace(environmentName)) builder = builder.AddJsonFile($"appsettings.{environmentName}.json", true); builder = builder.AddEnvironmentVariables(); return builder.Build(); } }
這個工具類的使用方式就不再贅述了。