优点:所有数据库都支持
只能满足常规要求,个性化太高的用 234方案
//.net6以下 db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\1", "Models"); //.net6以上 string加? db.DbFirst.IsCreateAttribute().StringNullable().CreateClassFile("c:\\Demo\\1", "Models"); //参数1:路径 参数2:命名空间 //IsCreateAttribute 代表生成SqlSugar特性
新功能1:格式化文件名
db.DbFirst.FormatFileName(x => x.ToLower()).CreateClassFile("c\\"); //格式化类名和字段名 看标题6
新功能2: NET 7 字符串是否需要?设置
db.DbFirst.StringNullable().CreateClassFile("c\\");//强制可以空类型string加上?
db.DbFirst.Where("Student").CreateClassFile("c:\\Demo\\2", "Models"); db.DbFirst.Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\3", "Models"); db.DbFirst.Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\4", "Models");
db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\5", "Models");
db.DbFirst.IsCreateDefaultValue().CreateClassFile("c:\\Demo\\6", "Demo.Models"); //5.1.4.108-preview12+ 支持了替换字符串 db.DbFirst.Where("Student") .CreatedReplaceClassString(it=>it.Replace("xxx","yyy"))// 也可以用正则 Regex.Replace .CreateClassFile("c:\\Demo\\2", "Models");
添加 SettingPropertyTemplate重载,加强自定义属性的定义功能
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\\7");
注意:该功能可能和IsCreateAttribute存在冲突一般不要一起使用
新功能:5.1.4.115
db.DbFirst .IsCreateAttribute()//创建sqlsugar自带特性 .FormatFileName(it => "File_" + it) //格式化文件名(文件名和表名不一样情况) .FormatClassName(it => "Class_" + it)//格式化类名 (类名和表名不一样的情况) .FormatPropertyName(it => "Property_" + it)//格式化属性名 (属性名和字段名不一样情况) .CreateClassFile("c:\\Demo\\4", "Models"); //注意只能写一个 //正确 FormatFileName(it=>it.Replace(" ","").Replace("-","_")) //错误 .FormatFileName(it=>it.Replace(" ","")) .FormatFileName(it=>it.Replace("-","_"))
这个替换性能损耗最大,能用其他功能替换优先其他功能替换
//5.1.4.108-preview12+ 支持了替换字符串 db.DbFirst.Where("Student") .CreatedReplaceClassString(it=>it.Replace("xxx","yyy"))//也可以用正则 Regex.Replace .CreateClassFile("c:\\Demo\\2", "Models");
db.DbFirst.Where("order").SettingClassDescriptionTemplate(it => { return it+"\r\n [Tenant(\""+db.CurrentConnectionConfig.ConfigId+"\")]"; }).CreateClassFile("c:\\Demo\\1", "Models");
db.DbFirst.StringNullable().CreateClassFile("c\\");//强制可以空类型string加上?
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true, ConfigureExternalServices = new ConfigureExternalServices() { RazorService = new RazorService()//新建一个RazorService类 } }); var templte = RazorFirst.DefaultRazorClassTemplate;//这个是自带的,这个模版可以修改 db.DbFirst.UseRazorAnalysis(templte).CreateClassFile("c:\\Demo\\Razor\\");
RazorService 类在framework和.net Core中小有区别看下面例子
创建RazorService 需要安装RazorEngine 3.10.0.0
using RazorEngine; using RazorEngine.Templating; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SqlSugar.DbFirstExtensions { public class RazorService : IRazorService { public List<KeyValuePair<string,string>> GetClassStringList(string razorTemplate, List<RazorTableInfo> model) { if (model != null && model.Any()) { var result = new List<KeyValuePair<string, string>>(); foreach (var item in model) { try { item.ClassName = item.DbTableName;//格式化类名 string key = "RazorService.GetClassStringList"+ razorTemplate.Length; var classString = Engine.Razor.RunCompile(razorTemplate, key, item.GetType(), item); result.Add(new KeyValuePair<string,string>(item.ClassName,classString)); } catch (Exception ex) { new Exception(item.DbTableName + " error ." + ex.Message); } } return result; } else { return new List<KeyValuePair<string, string>> (); } } } }
创建RazorService 需要安装 RazorEngine.NetCore 3.1
using RazorEngine; using RazorEngine.Templating; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; namespace DbFirstRazorTest { class Program { static void Main(string[] args) { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST", DbType = DbType.SqlServer, IsAutoCloseConnection = true, ConfigureExternalServices = new ConfigureExternalServices() { RazorService = new RazorService() } }); db.DbFirst.UseRazorAnalysis(RazorFirst.DefaultRazorClassTemplate).CreateClassFile("c:\\Demo\\Razor\\"); } } public class RazorService : IRazorService { public List<KeyValuePair<string, string>> GetClassStringList(string razorTemplate, List<RazorTableInfo> model) { if (model != null && model.Any()) { var result = new List<KeyValuePair<string, string>>(); foreach (var item in model) { try { item.ClassName = item.DbTableName;//格式化类名 string key = "RazorService.GetClassStringList" + razorTemplate.Length; var classString = Engine.Razor.RunCompile(razorTemplate, key, item.GetType(), item); result.Add(new KeyValuePair<string, string>(item.ClassName, classString)); } catch (Exception ex) { new Exception(item.DbTableName + " error ." + ex.Message); } } return result; } else { return new List<KeyValuePair<string, string>>(); } } } }
缺点:只支持常用数据库 SqlServer、MySql、 Pgsql 、Oracle、Sqlite、达梦 和 金仓(默认模式)
优点: 界面操作 、修改模版方便
下面方法可以拿到表信息
//例1 获取所有表 var tables = db.DbMaintenance.GetTableInfoList(false);//true 走缓存 false不走缓存 foreach (var table in tables) { Console.WriteLine(table.Description);//输出表信息 }
下面方法可以拿到列信息
db.DbMaintenance.GetColumnInfosByTableName(表名, false)
2016 © donet5.comApache Licence 2.0