TIL

Today I Learned


Project maintained by gushwell Hosted on GitHub Pages — Theme by mattgraham

DbContextを多段継承する時のTips


継承元

protectedなコンストラクタを定義する。

public class BaseDbContext : DbContext {
    public BaseDbContext(DbContextOptions<BaseDbContext> options)
        : base(options) {
    }

    protected BaseDbContext(DbContextOptions options)
        : base(options) {
    }
}

派生先

継承元のBaseDbContextから派生する。

public class DelivedDbContext : BaseDbContext {
    public DelivedDbContext (DbContextOptions<DelivedDbContext> options)
        : base(options) {
    }
}

baseでは、protectedなコンストラクタが呼ばれる。

参考:
https://github.com/dotnet/efcore/issues/7533#issuecomment-353669263