无实体建表,部分默认值报错 返回

SqlSugar 沟通中
6 329
该叫什么 飞羽 发布于2023/9/18
悬赏:0 飞吻



            Console.WriteLine($"开始备份表结构:{tablename}");

            if (ToDb.DbMaintenance.IsAnyTable(tablename))
            {
                Console.WriteLine($"开始备份表结构:{tablename},已经存在无需备份");
                return;
            }
            try
            {
                List<DbColumnInfo> list = FromDb.DbMaintenance.GetColumnInfosByTableName(tablename);
                string[] data = { "int", "bigint", "bit", "ntext", "text", "timestamp", "timestamp", "date", "datetime", "datetime2", "smalldatetime", "tinyint", "variant", "uniqueidentifier" };
                var typeBilder = FromDb.DynamicBuilder().CreateClass(tablename, new SugarTable() { });
                foreach (var item in list)
                {
                    var columtype = Helper.GetType(item.DataType);
                    if (data.Contains(item.DataType))
                    {
                        typeBilder.CreateProperty(item.DbColumnName, columtype, new SugarColumn() { ColumnDataType = item.DataType, ColumnDescription = item.ColumnDescription, IsPrimaryKey = item.IsPrimarykey, IsNullable = item.IsNullable, IsIdentity = item.IsIdentity, DefaultValue = item.DefaultValue });
                    }
                    else
                    {
                        typeBilder.CreateProperty(item.DbColumnName, columtype, new SugarColumn() { ColumnDataType = item.DataType, Length = item.Length, ColumnDescription = item.ColumnDescription, IsPrimaryKey = item.IsPrimarykey, IsNullable = item.IsNullable, IsIdentity = item.IsIdentity  });

                    }
                }
                //创建类
                var type = typeBilder.BuilderType();//想缓存有typeBilder.WithCache
                ToDb.CodeFirst.InitTables(type); //建表属性API看迁移









报错时,打印的sql:


alter table [user] ADD DEFAULT '('1900-01-01')' FOR [birthday_update_time]

错误提示

{"“1900”附近有语法错误。"}

热忱回答6

  • fate sta fate sta VIP0
    2023/9/18

    不能直接查了用,默认值 有些需要自个处理

    0 回复
  • 飞羽 飞羽 VIP0
    2023/9/18

    默认值不能直接用么,有什么解决方法么,想用它来做一个数据库备份工具

    0 回复
  • fate sta fate sta VIP0
    2023/9/18

    @飞羽:有些格式比较复杂,目前只做到了读出默认值

    0 回复
  • fate sta fate sta VIP0
    2023/9/18

    你什么数据库我可以针对你的库帮你优化一下支持一下

    0 回复
  • 飞羽 飞羽 VIP0
    2023/9/19

    sqlsever,好的麻烦你了

    0 回复
  • 飞羽 飞羽 VIP0
    2023/9/19

    非常感谢

    0 回复