ConfigurationDbContext是否有可用的扩展,我们可以在使用IdentityServer4时为任何其他字段或索引自定义上下文?
是否可以扩展IdentityServer4提供的模型?
答案 0 :(得分:1)
我猜你问的是如何自定义EntityFramework.DbContexts.Config
您可以从以下代码中获得一些想法
public class CustomConfigurationContextFactory : IDbContextFactory<CustomConfigurationDbContext>
{
/// <summary>
/// A factory to create an instance of the StudentsContext
/// </summary>
/// <param name="connectionString"></param>
/// <returns></returns>
public static CustomConfigurationDbContext Create(string connectionString)
{
var optionsBuilder = new DbContextOptionsBuilder<ConfigurationDbContext>();
optionsBuilder.UseSqlServer(connectionString);
var context = new CustomConfigurationDbContext(
optionsBuilder.Options,
new ConfigurationStoreOptions());
context.Database.EnsureCreated();
return context;
}
public CustomConfigurationDbContext Create(DbContextFactoryOptions options)
{
var context = new CustomConfigurationDbContext(
new DbContextOptions<ConfigurationDbContext>(),
new ConfigurationStoreOptions()
);
context.Database.EnsureCreated();
return context;
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ConfigureClientContext(_storeOptions);
builder.Entity<Client>().HasIndex(x => x.ClientName).IsUnique();
}
}
public class CustomConfigurationContextFactory : IDbContextFactory<CustomConfigurationDbContext>
{
public CustomConfigurationDbContext Create(DbContextFactoryOptions options)
{
var context = new CustomConfigurationDbContext(
new DbContextOptions<ConfigurationDbContext>(),
new ConfigurationStoreOptions()
);
context.Database.EnsureCreated();
return context;
}
}
OnModelCreating
您可以添加索引,也可以将自定义Dbset添加到自定义配置dbcontext
答案 1 :(得分:0)
您可以在 Asp.net Core 中扩展这样的ConfigurationDbContext类:
public class MyNewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
public class MyOtherNewModel
{
public int Id { get; set; }
public ApiResource ApiResource { get; set; }//<-- This is built-in model for identityserver4.
public MyNewModel MyNewModel { get; set; }
public List<ApiClaims> ApiClaims {get; set;} //<-- This will add a "MyOtherNewModelId" field into built-in ApiClaims table.
}
public class IdentityProviderDbContext : ConfigurationDbContext
{
public IdentityProviderDbContext(DbContextOptions<ConfigurationDbContext> options, ConfigurationStoreOptions storeOptions) : base(options, storeOptions)
{ }
public DbSet<MyNewModel> MyNewModelTable { get; set; }
public DbSet<MyOtherNewModel> MyOtherNewModelTable { get; set; }
//...
}
然后我们可以在控制台中使用以下命令:
add-migration InitialDeriveFromBaseClass -context IdentityProviderDbContext
您还可以设法在一个单独的类库中对其进行扩展,如下所示:
1-创建一个新的.net核心类库。
2-像这样更改上面的IdentityProviderDbContext
类:
public class IdentityProviderDbContext : ConfigurationDbContext
{
//TODO: This connecton string will be removed as soon as possible.
string connectionString = "Server=.;Database=SomeDb;Trusted_Connection=True;MultipleActiveResultSets=true";
public IdentityProviderDbContext(DbContextOptions<ConfigurationDbContext> options)
: base(options,
new ConfigurationStoreOptions()
{
ConfigureDbContext = configureOptions =>
configureOptions.UseSqlServer("Server=.;Database=SomeDb;Trusted_Connection=True;MultipleActiveResultSets=true",
sql => sql.MigrationsAssembly(typeof(IdentityProviderDbContext).GetTypeInfo().Assembly.GetName().Name))
})
{ }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(connectionString);
}
//...
// The other parts are the same as above code snippet.
也许我们应该以更适当的方式(更友好SOLID)进行连接字符串。但这只是一个例子。
3-添加一个名为IdentityProviderDbContextFactory
的新类,以通知Entity Framework如何生成迁移。
class IdentityProviderDbContextFactory : IDesignTimeDbContextFactory<IdentityProviderDbContext>
{
public IdentityProviderDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ConfigurationDbContext>();
optionsBuilder.UseSqlServer("Server=.;Database=SomeDb;Trusted_Connection=True;MultipleActiveResultSets=true");
return new IdentityProviderDbContext(optionsBuilder.Options);
}
}
4-运行add-migration命令
和完成。
如果要将字段添加到现有的内置模型中,也许应该阻止EF生成original class model
(在OnConfiguring()
或其他位置),而引入derived model
(来自该original model
)