【问题标题】:Database Schema not changing at Runtime in Asp.net Core 2.2 & Entity Framework Core数据库架构在 Asp.net Core 2.2 和 Entity Framework Core 的运行时没有改变
【发布时间】:2019-06-20 10:06:31
【问题描述】:

我有一个应用程序,其中数据保存在不同用户的不同 sql 模式中。

例如

用户 1 的数据保存在 SCHEMA1 中

用户 2 的数据保存在 SCHEMA2 中

以前的应用程序是在 MVC 3 中开发的,它运行良好且符合预期。 现在,我们正在迁移 .Net Core 2.2 中的应用程序,其中此功能不起作用
.net 核心没有IDbModelCacheKeyProvider,因为只有一个架构在工作

下面是 DBContext 文件

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    //public string Schema { get; set; }
    private readonly IConfiguration configuration;
    public string SchemaName { get; set; }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }

    public ApplicationDbContext(string schemaname)
        : base()
    {
        SchemaName = schemaname;
    }

    public DbSet<EmployeeDetail> EmployeeDetail { get; set; }



    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                      .AddJsonFile("appsettings.json");
        var configuration = builder.Build();

        optionsBuilder.UseSqlServer(configuration["ConnectionStrings:SchemaDBConnection"]);

        var serviceProvider = new ServiceCollection().AddEntityFrameworkSqlServer()
                            .AddTransient<IModelCustomizer, SchemaContextCustomize>()
                            .BuildServiceProvider();
    }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.RemovePluralizingTableNameConvention();
        modelBuilder.HasDefaultSchema(SchemaName);
        base.OnModelCreating(modelBuilder);
    }

    public string CacheKey
    {
        get { return SchemaName; }
    }


}   


public class SchemaContextCustomize : ModelCustomizer
{
    public SchemaContextCustomize(ModelCustomizerDependencies dependencies)
        : base(dependencies)
    {

    }
    public override void Customize(ModelBuilder modelBuilder, DbContext dbContext)
    {
        base.Customize(modelBuilder, dbContext);

        string schemaName = (dbContext as ApplicationDbContext).SchemaName;
        (dbContext as ApplicationDbContext).SchemaName = schemaName;

    }
}

我的问题是如何在运行时更改 schemaName

那么组织该机制的正确方法是什么:

通过用户凭据找出架构名称; 从特定架构的数据库中获取用户特定的数据。

【问题讨论】:

  • IDbModelCacheKeyProvider 不是关于模式的。您在 MVC 3 中使用的是一种解决方法,而不是在相同上下文中使用不同模式的正确方法
  • @PanagiotisKanavos 感谢您提供信息,我已经设法使用自定义 UserManager 和以下上下文类解决了这个问题。如果可能的话,您能否分享示例或 URL 以实现具有相同上下文的不同模式,这将很有帮助

标签: c# asp.net asp.net-mvc asp.net-core entity-framework-core


【解决方案1】:

我能够通过更改 onConfiguring 方法在运行时更改架构

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

    public string SchemaName { get; set; }


    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }

    public ApplicationDbContext(string schemaname)
        : base()
    {
        SchemaName = schemaname;
    }

    public DbSet<EmployeeDetail> EmployeeDetail { get; set; }



    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {

        var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                      .AddJsonFile("appsettings.json");
        var configuration = builder.Build();

        var serviceProvider = new ServiceCollection().AddEntityFrameworkSqlServer()
                            .AddSingleton<IModelCustomizer, SchemaContextCustomize>()
                            .BuildServiceProvider();
        optionsBuilder.UseSqlServer(configuration["ConnectionStrings:SchemaDBConnection"]).UseInternalServiceProvider(serviceProvider);
    }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
       // modelBuilder.MapProduct(SchemaName);

        modelBuilder.RemovePluralizingTableNameConvention();
        if (!string.IsNullOrEmpty(SchemaName))
        {
            modelBuilder.HasDefaultSchema(SchemaName);
        }
        base.OnModelCreating(modelBuilder);
    }

    public string CacheKey
    {
        get { return SchemaName; }
    }
public class SchemaContextCustomize : ModelCustomizer
{
    public SchemaContextCustomize(ModelCustomizerDependencies dependencies)
        : base(dependencies)
    {

    }
    public override void Customize(ModelBuilder modelBuilder, DbContext dbContext)
    {
        base.Customize(modelBuilder, dbContext);

        string schemaName = (dbContext as ApplicationDbContext).SchemaName;
        (dbContext as ApplicationDbContext).SchemaName = schemaName;

    }
}
}

【讨论】:

  • 嗨。我知道这已经快一年了,但我不明白你实施的改变对你有什么帮助。我能看到的唯一(重要)区别是您将 SchemaContextCustomize 的时间跨度从 Transient 更改为 Singleton。甚至使用了 CacheKey 属性吗?您如何处理迁移(假设您使用它们)?
【解决方案2】:

最好的方法是使用多租户架构,以便能够为每个用户(租户)使用数据库架构
此架构推荐用于 Saas 应用程序


概念

让我们就一些基本概念达成一致:

【讨论】:

  • 这与问题关系不大。 OP 询问如何更改上下文使用的架构,以连接到正确的租户架构
  • 在他的问题中他说:“那么组织该机制的正确方法是什么:”,我试图解释概念和哲学,而不是给他一些代码......跨度>
  • OP已经使用具有多个模式的多租户架构。唯一有帮助的是一篇文章的链接,该文章解释了多种不同的技术,其中只有一种与多种模式有关。一个有用的答案将解释做什么和为什么,并包括相关的代码。
猜你喜欢
  • 1970-01-01
  • 2018-09-27
  • 2017-05-16
  • 2020-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
相关资源
最近更新 更多