dotnetcore / dotnetcore/FreeSql

希望在注册freesql的时候能够配置schema

Open
#2,222 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.4k
Forks
910
PR merge metrics
No merged PRs in 30d

Description

希望能默认支持,我们国产化数据库比较多,特别是pg类型的国产化数据库,最容易碰到。向作者致敬,注工作顺利
目前自我实现方式:
1、有实体映射数据表
public static IFreeSql UseGlobalSchema(this IFreeSql fsql)
{
var schema = System.Environment.GetEnvironmentVariable("db_schema");
if (!string.IsNullOrWhiteSpace(schema))
{
// 扫描所有程序集中带[Table]特性的实体(可根据需要添加程序集筛选)
var entities = GetTableEntities();
foreach (var type in entities)
{
fsql.CodeFirst.ConfigEntity(type, cfg =>
{
var tabeName = type.GetCustomAttribute(false)?.Name ?? string.Empty;
cfg.Name($"{schema}.{tabeName}");
});
}
return fsql;
}
return fsql;
}
2、无实体映射数据表
var schema = System.Environment.GetEnvironmentVariable("db_schema");
freeSql.Aop.ConfigEntity += (s, e) =>
{
// 处理数据库模式
if (!string.IsNullOrWhiteSpace(schema))
{
var tabname = e.ModifyResult.Name;
if (!string.IsNullOrEmpty(e.ModifyResult.Name))
{
var spbit = e.ModifyResult.Name.Split('.');
if (spbit.Length == 1) // 说明没有拼接模式
{
e.ModifyResult.Name = $"{schema}.{tabname}";
}
}
}
};

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.