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
https://www.donet5.com/Home/Doc
MIT License
5.35k stars 1.34k forks source link

使用 .DbFirst.SettingPropertyTemplate时vs2022提示尾行需要格式化和命名空间要一个个手工加入的问题 #1019

Closed 12500540 closed 2 years ago

12500540 commented 2 years ago

.SettingPropertyTemplate((columns, temp, type) => //属性:新重载 完全自定义用配置 { var columnattribute = "\r [SugarColumn({0})]";//此处代码生成数据实体有问题 List attributes = new List(); if (columns.IsPrimarykey) attributes.Add("IsPrimaryKey=true"); if (columns.IsIdentity) attributes.Add("IsIdentity=true"); if (attributes.Count == 0) { columnattribute = ""; } return temp.Replace("{PropertyType}", type) .Replace("{PropertyName}", columns.DbColumnName) .Replace("{SugarColumn}", string.Format(columnattribute, string.Join(",", attributes))); }) 复盘环境: 1、IDE是VS2022@17.1.3版 2、插件是SqlSugarCore@5.0.7.8版 3、类库是NET6框架 解决方案: var columnattribute = "\r\n [SqlSugar.SugarColumn({0})]"; //使用此代码替换原代码即可解决问题 前提: models类库已经nuget了SqlSugarCore或SqlSugar,根据coer或Freamwork框架而定 分析: 问题1:可能是VS2022 中文版换行必须使用 \r\n 问题2:引用空间问题,目前没办法找到上级节点,否则直接使用 using.SqlSugar 方式解决最完美,在属性上使用有点冗余代码

DotNetNext commented 2 years ago

应该是这样写的吧

        //属性:新重载 完全自定义用配置
        .SettingPropertyTemplate((columns,temp,type) => {

        var columnattribute = "\r           [SugarColumn({0})]";
        List<string> attributes = new List<string>();
        if (columns.IsPrimarykey)
            attributes.Add("IsPrimaryKey=true");
        if (columns.IsIdentity)
            attributes.Add("IsIdentity=true");
        if (attributes.Count == 0) 
        {
            columnattribute = "";
        }
        return temp.Replace("{PropertyType}", type)
                    .Replace("{PropertyName}", columns.DbColumnName)
                    .Replace("{SugarColumn}",string.Format(columnattribute,string.Join(",", attributes)));
          })

       .CreateClassFile("c:\\Demo\\7");
12500540 commented 2 years ago

应该是这样写的吧

        //属性:新重载 完全自定义用配置
        .SettingPropertyTemplate((columns,temp,type) => {

        var columnattribute = "\r           [SugarColumn({0})]";
        List<string> attributes = new List<string>();
        if (columns.IsPrimarykey)
            attributes.Add("IsPrimaryKey=true");
        if (columns.IsIdentity)
            attributes.Add("IsIdentity=true");
        if (attributes.Count == 0) 
        {
            columnattribute = "";
        }
        return temp.Replace("{PropertyType}", type)
                    .Replace("{PropertyName}", columns.DbColumnName)
                    .Replace("{SugarColumn}",string.Format(columnattribute,string.Join(",", attributes)));
          })

       .CreateClassFile("c:\\Demo\\7");

是的。是官网demo代码的小bug

DotNetNext commented 2 years ago

什么BUG我一直没有明白,我这边测试都正常 你可以加我 qq 610262374 细说

12500540 commented 2 years ago

db.DbFirst //类 .SettingClassTemplate(old => { return old;/修改old值替换/ }) //类构造函数 .SettingConstructorTemplate(old => { return old;/修改old值替换/ }) .SettingNamespaceTemplate(old => { return old + "\r\nusing SqlSugar;"; //追加引用SqlSugar }) //属性备注 .SettingPropertyDescriptionTemplate(old => { return old;/修改old值替换/})

       //属性:新重载 完全自定义用配置
       .SettingPropertyTemplate((columns, temp, type) => {

           var columnattribute = "\r\n           [SugarColumn({0})]";
           List<string> attributes = new List<string>();
           if (columns.IsPrimarykey)
               attributes.Add("IsPrimaryKey=true");
           if (columns.IsIdentity)
               attributes.Add("IsIdentity=true");
           if (attributes.Count == 0)
           {
               columnattribute = "";
           }
           return temp.Replace("{PropertyType}", type)
                        .Replace("{PropertyName}", columns.DbColumnName)
                        .Replace("{SugarColumn}", string.Format(columnattribute, string.Join(",", attributes)));
       })

      .CreateClassFile("c:\\Demo\\8");

根据dotnet5提供的新DEMO,可以完美解决遇到的两个问题,我认为可以Close这个issues了。