Skip to content
果糖网 edited this page Jun 23, 2024 · 10 revisions

1. Demo

//.net framework  
db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\1", "Models");
 
 //.net6+ string?
db.DbFirst.IsCreateAttribute().StringNullable().CreateClassFile("c:\\Demo\\1", "Models");

2. Generate entities with filters

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");

3. Generate entities with default values

db.DbFirst.IsCreateDefaultValue().CreateClassFile("c:\\Demo\\6", "Demo.Models");

db.DbFirst.Where("Student")
CreatedReplaceClassString (it = > it. Replace (" XXX, "" yyy")) / / can also use regular Regex. Replace
.CreateClassFile("c:\\Demo\\2", "Models");
  1. Customize the formatting function Add a SettingPropertyTemplate overload (which is weak above) to strengthen the definition of custom properties
db.DbFirst
// class
.SettingClassTemplate(old => { return old; /* Modify old value Replace */})
// Class constructor
.SettingConstructorTemplate(old =>{return old; /* Modify old value Replace */})
.SettingNamespaceTemplate(old => {
return old + "\r\nusing SqlSugar;" ; // Append references to SqlSugar
})
// Property Remarks
.SettingPropertyDescriptionTemplate(old =>{ return old; /* Modify old value Replace */})

// Properties: New overload fully custom configuration
.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");
Note: This feature may conflict with IsCreateAttribute. Do not use it together
  1. Format the class name and attribute name
db.DbFirst
.IsCreateAttribute()// Create sqlsugar features
.FormatFileName(it => "File_" + it) // formatfilename (file name is different from table name)
.FormatClassName(it => "Class_" + it)// Formatting the class name (if the class name is different from the table name)
.FormatPropertyName(it => "Property_" + it)// Formatting property name (property name and field name is different)
.CreateClassFile("c:\\Demo\\4", "Models");
  1. Replace the generated ClassString This replacement consumes the most performance and can be replaced with other functions before other functions

//5.1.4.108-preview12+ supports replacement strings db.DbFirst.Where("Student") CreatedReplaceClassString (it = > it. Replace (" XXX, "" yyy")) / / can also use regular Regex. Replace .CreateClassFile("c:\Demo\2", "Models");

  1. Add a tenant db.DbFirst.Where("order").SettingClassDescriptionTemplate(it => { return it+"\r\n [Tenant(""+db.CurrentConnectionConfig.ConfigId+"")]"; }).CreateClassFile("c:\Demo\1", "Models");
  2. Generate a String? .NET 7+ db.DbFirst.StringNullable().CreateClassFile("c\"); // Can the null type string be added?
Clone this wiki locally