EF Core – Library use EF

前言

写 Library 有时候会用到 database, 会想用 EF 来维护.

比如 Identity, IdentityServer, OpenIddict, 这些 Library 都有使用到 EF.

虽然大家都用 EF, 但它们使用的手法都不太一样哦.

这篇就来研究一下, 写 Library 的时候怎么去使用 EF.

 

主要参考

IdentityServer 源码

Openiddict 源码

AutoHistory 源码

相关 Class Library EF Core 的文章

.NET Core — Using Entity Framework Core in a separate Project

Design-time DbContext Creation

A library to run your business logic when using Entity Framework Core

Using EF Core in a Separate Class Library

Using EF Core in a Separate Class Library project

 

4 种手法

不要问我为什么有这么多 pattern. 我也想知道.

继承方式

Identity 的做法是用继承, ApplicationDbContext 继承 IdentityDbContext

我不太欣赏这种方式 (就不多介绍了). 但是不只 identity 这样用哦, 有些 library 也是用这个方式

 

多个 DbContext

IdentityServer 用的方式是多个 DbContext, 出来的 Migrations folder 长这样

我觉得这个方案挺不错的, 分得很开.

首先, Library 定义好 LibraryDbContext, Entity 等

然后 Application startup,cs

var assemblyName = typeof(Program).GetTypeInfo().Assembly.GetName().Name;
builder.Services.AddDbContext<LibraryDbContext>(options =>
    options.UseSqlServer(connectionString, o => o.MigrationsAssembly(assemblyName))
);

这里只是写个 demo 我就没有封装太美了, 关键就是那个 options.UseSqlServer 要让 Application 负责就可以了. 

还有一点就是一定要配上 MigrationsAssembly. 

 

ReplaceService IModelCustomizer

OpenIddict 用的是 ReplaceService

 然后

 然后

 这个方案我也是不太喜欢 (就不多介绍了), 要知道 ReplaceService 只能用一次丫, 你用了其它 Library 也要用就完了.

关于这个方案也可以看这篇 Using Entity Framework Core IModelCustomizer to target multiple data stores

还有它的 limitation

 

ModelBuilder 扩展

这个是我在 AutoHistory 插件看见的

拦截到了, modelBuilder 就可以用来做 config 了

虽然写不了 DbSet 这种东西, 但是不要紧.

public DbSet<Product> Products => Set<Product>();

调用的时候可以用泛型 Type

我觉得这个方案算是最简单的了. 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章