DotNetNext / SqlSugar

.Net aot ORM Fastest ORM Simple Easy VB.NET Sqlite orm Oracle ORM Mysql Orm 虚谷数据库 postgresql ORm SqlServer oRm 达梦 ORM 人大金仓 ORM 神通ORM C# ORM , C# ORM .NET ORM NET5 ORM .NET6 ORM ClickHouse orm QuestDb ,TDengine ORM,OceanBase orm,GaussDB orm ,Tidb orm Object/Relational Mapping

Home Page:https://www.donet5.com/Home/Doc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

如何根据不同的数据库类型更改表名或列名

aaa930811 opened this issue · comments

commented

我有一个实体Doc,它在mysql数据库中表名叫Doc1,它在sqlserver数据库中表名叫Doc2,想请教一下可以根据数据库类型自动更改表名和列名吗?

[SugarTable("Doc1")]
    public class Doc
    {
        [SugarColumn(IsPrimaryKey = true, IndexGroupNameList = new string[] { "FileUID" })]
        public Guid FileUID { get; set; }
        public Guid ObservationUID { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }
        public string PatientClass { get; set; }
        public bool DeleteFlag { get; set; }
    }
[SugarTable("Doc2")]
    public class Doc
    {
        [SugarColumn(IsPrimaryKey = true, IndexGroupNameList = new string[] { "FileUID" })]
        public Guid FileUID { get; set; }
        public Guid ObservationUID { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }

        [SugarColumn(ColumnName ="PtClass")]
        public string PatientClass { get; set; }
        public bool DeleteFlag { get; set; }
    }
commented

是否可以这样写?

_dbClient = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = config.DbType,
                ConnectionString = config.ConnectionString,
                IsAutoCloseConnection = true,
                ConfigureExternalServices = new ConfigureExternalServices
                {
                    EntityService = (property, column) =>
                    {
                        var attributes = property.GetCustomAttributes(true);
                        if (attributes.Any(it => it is Column_PatientClassAttribute))
                        {
                            column.DbColumnName = config.DbType == DbType.SqlServer ? "PtClass" : "PatientClass";
                        }
                    },
                    EntityNameService = (type, entity) =>
                    {
                        var attributes = type.GetCustomAttributes(true);
                        if (attributes.Any(it => it is Table_DocAttribute))
                        {
                            entity.DbTableName = config.DbType == DbType.SqlServer ? "Doc2" : "Doc1";
                        }
                    }
                }
[Table_Doc]
public class Doc
{
    [SugarColumn(IsPrimaryKey = true, IndexGroupNameList = new string[] { "FileUID" })]
    public Guid FileUID { get; set; }
    public Guid ObservationUID { get; set; }
    public string Name { get; set; }
    public string Sex { get; set; }

    [Column_PatientClass]
    public string PatientClass { get; set; }
    public bool DeleteFlag { get; set; }
}

public class Table_DocAttribute : Attribute
{
}

public class Column_PatientClassAttribute : Attribute
{
}

升级最5.1.4.91+版本支持了多库 EntityService

也就是不同库是不同的实体AOP

SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig(){
ConnectionString = "连接符字串",
DbType = DbType.SqlServer,
IsAutoCloseConnection = true},
db=>{
db.GetConnection("a").CurrentCoufig.ConfigureExternalServices =new ConfigureExternalServices (){}
db.GetConnection("b").CurrentCounfig.ConfigureExternalServices =new ConfigureExternalServices (){}
});
差不多这样,不清楚你理解了没有

通过修改配置的方式实现

commented

我的程序只需要连接一个数据库,业务数据库是分2种,一种是mysql的一种是sqlserver的,按照以前不用ORM框架的写法的话我的代码分为mysql分支和sqlserver分支,现在想整合到一起

commented

我有一个实体Doc,它在mysql数据库中表名叫Doc1,它在sqlserver数据库中表名叫Doc2,想请教一下可以根据数据库类型自动更改表名和列名吗?

[SugarTable("Doc1")]
    public class Doc
    {
        [SugarColumn(IsPrimaryKey = true, IndexGroupNameList = new string[] { "FileUID" })]
        public Guid FileUID { get; set; }
        public Guid ObservationUID { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }
        public string PatientClass { get; set; }
        public bool DeleteFlag { get; set; }
    }
[SugarTable("Doc2")]
    public class Doc
    {
        [SugarColumn(IsPrimaryKey = true, IndexGroupNameList = new string[] { "FileUID" })]
        public Guid FileUID { get; set; }
        public Guid ObservationUID { get; set; }
        public string Name { get; set; }
        public string Sex { get; set; }

        [SugarColumn(ColumnName ="PtClass")]
        public string PatientClass { get; set; }
        public bool DeleteFlag { get; set; }
    }

想这样做就是为了合并2个分支好兼容2种不同的数据库类型

我写的你没看动懂吗,当然是可以的

SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig(){
ConnectionString = "连接符字串",
DbType = DbType.SqlServer,
IsAutoCloseConnection = true},
db=>{
db.GetConnection("a").CurrentCoufig.ConfigureExternalServices =new ConfigureExternalServices (){写改a库表名}
db.GetConnection("b").CurrentCounfig.ConfigureExternalServices =new ConfigureExternalServices (){写改b库表名}
});

5.1.4.91+版本

EntityService = (property, column) =>
{
var attributes = property.GetCustomAttributes(true);
if (attributes.Any(it => it is Column_PatientClassAttribute))
{
column.DbColumnName = config.DbType == DbType.SqlServer ? "PtClass" : "PatientClass";
}
},

只要这样就行了