c# - 在 Asp.net Core 2.2 和 Entity Framework Core 中,数据库架构在运行时不会更改

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

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

例如

用户 1 数据保存在 SCHEMA1 中

用户 2 数据保存在 SCHEMA2 中

以前的应用程序是在 MVC 3 中开发的,并且运行良好且符合预期。 现在我们正在迁移 .Net Core 2.2 中的应用程序,其中此功能不起作用
.net core 没有 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

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

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

最佳答案

我能够通过更改 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;

    }
}
}

关于c# - 在 Asp.net Core 2.2 和 Entity Framework Core 中,数据库架构在运行时不会更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56683629/

相关文章:

c# - 强类型对象列表到动态列表

c# - 使用 LINQ 对对象列表进行分页

java - Java 中是否有等效的 ASP.NET 控件缓存和 Post-cache 替换

javascript - Jquery 在 cshtml 页面中不工作

c# - Oracle Data Provider for .NET 不支持 Oracle 19.0.96.0.0

java - 串口发送命令,MVC应用

c# - 从 C# 驱动程序更新字段

c# - 改进 WPF 可视层中的 HitTest

c# - 无法调用接口(interface)的已实现函数(名称在当前上下文中不存在)

asp.net - ASP.NET 应用程序中的 Google 桌面或索引文本文件搜索