sharding/samples/Samples.DynamicDb.Npgsql/WebApplication1/Pages/CreateDbKey.cshtml.cs

49 lines
1.4 KiB
C#
Raw Normal View History

2022-06-21 16:04:56 +08:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ShardingCore.Helpers;
using System;
using System.Collections.Generic;
using WebApplication1.Data;
using WebApplication1.Data.Helpers;
using WebApplication1.Data.Models;
using WebApplication1.Data.Sharding;
namespace WebApplication1.Pages
{
public class CreateDbKeyModel : PageModel
{
private readonly AbstaractShardingDbContext db;
[BindProperty]
2022-06-23 09:30:44 +08:00
public string Key { get; set; }
2022-06-21 16:04:56 +08:00
public CreateDbKeyModel(AbstaractShardingDbContext db)
{
this.db = db;
}
public void OnGet()
{
2022-06-23 09:30:44 +08:00
Key = "";
2022-06-21 16:04:56 +08:00
}
public IActionResult OnPost()
{
2022-06-23 09:30:44 +08:00
db.TestModelKeys.Add(new TestModelKey { Key = Key });
2022-06-21 16:04:56 +08:00
db.SaveChanges();
// <20><>ȡ<EFBFBD><C8A1>д<EFBFBD><EFBFBD><EBB5BD><EFBFBD><EFBFBD>
var dblist = JsonFileHelper.Read<List<string>>(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName);
2022-06-23 09:30:44 +08:00
dblist.Add(Key);
2022-06-21 16:04:56 +08:00
dblist.Sort();
JsonFileHelper.Save(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName, dblist);
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
2022-06-23 09:30:44 +08:00
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", Key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{Key};");
2022-06-21 16:04:56 +08:00
return RedirectToPage("DbKeyMan");
}
}
}