gpt4 book ai didi

c# - Asp.net Core 2.2 和 Entity Framework Core 中的数据库模式在运行时没有改变

转载 作者:行者123 更新时间:2023-11-30 22:53:56 29 4
gpt4 key购买 nike

我有一个应用程序,其中为不同的用户将数据保存在不同的 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

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

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

最佳答案

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com