CARVIEW |
Select Language
HTTP/2 200
date: Fri, 25 Jul 2025 00:07:24 GMT
content-type: text/html; charset=utf-8
cache-control: max-age=0, private, must-revalidate
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
link: ; rel=preload; as=fetch; crossorigin=use-credentials
referrer-policy: no-referrer-when-downgrade
server-timing: issue_layout-fragment;desc="issue_layout fragment";dur=220.859412,issue_conversation_content-fragment;desc="issue_conversation_content fragment";dur=513.644042,issue_conversation_sidebar-fragment;desc="issue_conversation_sidebar fragment";dur=41.891018,nginx;desc="NGINX";dur=0.608082,glb;desc="GLB";dur=101.43608
strict-transport-security: max-age=31536000; includeSubdomains; preload
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With, Accept,Accept-Encoding, Accept, X-Requested-With
x-content-type-options: nosniff
x-frame-options: deny
x-voltron-version: a2eb102
x-xss-protection: 0
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=BL4N3Z%2BP80xm1PJ7JT5epcGK7e%2Bec6ATkU9VQ3QvcleYHEtO7qD6F4j0O2LAk5Y9ZXTnnCXsTybK10mezTO24HNwBzzaiX9Q8oBaqPd8Rn73EKFwUEBWvb10LlrE%2BGuMcQcTZWmO5OuDnLBjKbJx9qGTe5XkopTnTyjlpIlJ8y3wi10n9ZRDfYAr5D4pXOsUoxKiS72AWnSJ3IvP5vx8T5zAKXL10CK8yrUNItZ213rSM60D%2Fnlb%2BgD7%2BKOpNZ1GLfrn3rJVBO2rkTNf4BZ9BQ%3D%3D--HiQdMlaUmKioFS1x--SPJc2dfPSrULnuX2zr8fmw%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.1621114802.1753402043; Path=/; Domain=github.com; Expires=Sat, 25 Jul 2026 00:07:23 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Sat, 25 Jul 2026 00:07:23 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: E0CA:1D2B8F:2FFE5:4BB3C:6882CABB
多个 IFreeSql 实例,如何注入使用? · Issue #44 · dotnetcore/FreeSql · GitHub
No one assignedNo typeNo projectsNo milestoneNone yetNo branches or pull requests
Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 885
Closed
Labels
docsThis is a documentThis is a document
Description
第一种方法:定义多个 IFreeSql
1、定义两个标识类:
class MySqlFlag {}
class SqlServerFlag {}
2、在 Startup.cs 中单例注入
public void ConfigureServices(IServiceCollection services)
{
var fsql1 = new FreeSqlBuilder().UseConnectionString(DataType.MySql, "str1")
.Build<MySqlFlag>();
var fsql2 = new FreeSqlBuilder().UseConnectionString(DataType.MySql, "str1")
.Build<SqlServerFlag>();
services.AddSingleton<IFreeSql<MySqlFlag>>(fsql1);
services.AddSingleton<IFreeSql<SqlServerFlag>>(fsql2);
}
3、在 Controller 中使用
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
public ValuesController(IFreeSql<MySqlFlag> mysql, IFreeSql<SqlServerFlag> sqlserver)
{
}
}
第二种方法:使用 FreeSql.Clound(推荐)
为 FreeSql 提供跨数据库访问,分布式事务TCC、SAGA解决方案,支持 .NET Core 2.1+, .NET Framework 4.0+.
开源地址:https://github.com/2881099/FreeSql.Cloud
dotnet add package FreeSql.Cloud
or
Install-Package FreeSql.Cloud
public enum DbEnum { db1, db2, db3 }
var fsql = new FreeSqlCloud<DbEnum>(); //提示:泛型可以传入 string
fsql.DistributeTrace = log => Console.WriteLine(log.Split('\n')[0].Trim());
fsql.Register(DbEnum.db1, () => new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, @"Data Source=db1.db")
.Build());
fsql.Register(DbEnum.db2, () => new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, @"Data Source=db2.db")
.Build());
fsql.Register(DbEnum.db3, () => new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, @"Data Source=db3.db")
.Build());
FreeSqlCloud 必须定义成单例模式
new FreeSqlCloud<DbEnum>() 多连接管理
new FreeSqlCloud<DbEnum>("myapp") 开启 TCC/SAGA 事务生效
FreeSqlCloud 的访问方式和 IFreeSql 一样:
fsql.Select<T>();
fsql.Insert<T>();
fsql.Update<T>();
fsql.Delete<T>();
//...
切换数据库:
fsql.Change(DbEnum.db3).Select<T>();
//同一线程,或异步await 后续 fsql.Select/Insert/Update/Delete 操作是 db3
fsql.Use(DbEnum.db3).Select<T>();
//单次有效
自动定向数据库配置:
//对 fsql.CRUD 方法名 + 实体类型 进行拦截,自动定向到对应的数据库,达到自动 Change 切换数据库目的
fsql.EntitySteering = (_, e) =>
{
switch (e.MethodName)
{
case "Select":
if (e.EntityType == typeof(T))
{
//查询 T 自动定向 db3
e.DBKey = DbEnum.db3;
}
else if (e.DBKey == DbEnum.db1)
{
//此处像不像读写分离?
var dbkeyIndex = new Random().Next(0, e.AvailableDBKeys.Length);
e.DBKey = e.AvailableDBKeys[dbkeyIndex]; //重新定向到其他 db
}
break;
case "Insert":
case "Update":
case "Delete":
case "InsertOrUpdate":
break;
}
};
doson2006, lively-qcy, NMSAzulX, cnkker, luoyunchong and 3 more
Metadata
Metadata
Assignees
Labels
docsThis is a documentThis is a document
Type
Projects
Milestone
Relationships
Development
Issue actions
You can’t perform that action at this time.