我有一个应用程序,其中数据保存在不同用户的不同 sql 模式中。
例如
用户 1 数据保存在 SCHEMA1
用户 2 数据保存在 SCHEMA2
以前的应用程序是在 MVC 3 中开发的,它运行良好且符合预期。
现在我们正在迁移 .Net Core 2.2 中的应用程序,其中该功能不起作用
.net 核心没有
下面是 DBContext 文件
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | { //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 方法在运行时更改架构
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | { 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; } } } |
最好的方法是使用多租户架构能够为每个用户(租户)使用数据库模式
建议将此架构用于 Saas 应用程序
概念
Leta€?s 同意一些基本概念:
本文将向您展示如何使用 .net 核心实现多租户应用程序:https://stackify.com/writing-multitenant-asp-net-core-applications/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://