簡體   English   中英

不支持關鍵字:'database=multitenant_clienta;trusted_connection'

[英]Keyword not supported: 'database=multitenant_clienta;trusted_connection'

我的程序是這樣的:-

在這里,我試圖從特定數據庫中查找數據表的數據列表。我的目標是:- 假設這個 URL 我在運行這個應用程序時單擊我的瀏覽器。 https://localhost:44308/ClientA/Contacts “ClienatA”是數據庫的名稱,“Contact”是該數據庫的表名。 我想顯示這個“聯系人”數據列表。我已經在我的 SQL 服務器系統地創建了一個數據庫來找到這個數據。我成功地使用多租戶進程動態更改了我的連接字符串名稱。但是當我運行這個應用程序時,我發現一個錯誤。 並且無法在我的視圖中顯示我的特定表格數據。

這是我的代碼:-

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=DESKTOP-N41V6ER\\SQLEXPRESS;Database=MultiTenant_Master;Trusted_Connection=True;MultipleActiveResultSets=true",
    "TemplateConnection": "Server=DESKTOP-N41V6ER\\SQLEXPRESS;Database==__DBNAME__;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

ApplicationDbContext.cs

  public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        public DbSet<Customer> Customer { get; set; }
        public DbSet<Contact> Contact { get; set; }
    }

啟動.cs

//other code
 services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));



            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddCustomerDbContext(Configuration);  //call for main work



            services.AddDefaultIdentity<IdentityUser>()
                .AddDefaultUI(UIFramework.Bootstrap4)
                .AddEntityFrameworkStores<ApplicationDbContext>();
//other code

CustomerDataContextExtensions.cs

 public static class CustomerDataContextExtensions
    {
        public static void AddCustomerDbContext(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddScoped(provider =>
            {
                var httpContext = provider.GetService<IHttpContextAccessor>();

                // In this sample we are using a customer identifier as the firs segment in the url request
                // Ex: http://localhost:5000/clienta/contacts
                //     http://localhost:5000/clientb/contacts

                var clientSlug = httpContext.HttpContext.Request.Path.Value.Split("/", StringSplitOptions.RemoveEmptyEntries)[0];


               

                var connString = configuration.GetConnectionString("TemplateConnection").Replace("__DBNAME__", $"MultiTenant_{clientSlug}");
                var opts = new DbContextOptionsBuilder<ApplicationDbContext>();
                //opts.UseSqlServer(connString, s => s.EnableRetryOnFailure());
                opts.UseSqlServer(connString,s=>s.EnableRetryOnFailure());
                opts.EnableSensitiveDataLogging();

                return new ApplicationDbContext(opts.Options);
            });
        }
    }

家庭控制器

 private readonly ApplicationDbContext _db;
        public HomeController(ApplicationDbContext db)
        {
            _db = db;
        }
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet("/{clientName}/contacts")]
        public async Task<IActionResult> GetContacts([FromRoute]string clientName)

        {
            var contacts = await _db.Contact.ToListAsync(); //This line actually made this error. I debugged.
            return View();
        }

當我運行應用程序時,我發現了這個錯誤:-

在此處輸入圖像描述

我如何解決這個問題。如何顯示我的特定表的數據。

您的模板中有兩個等號:

Database==__DBNAME__

刪除第一個:

Database=__DBNAME__

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM