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

Generate entity file

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.IsCreateAttribute().Where("Student").CreateClassFile("c:\\Demo\\2", "Models");
db.DbFirst.IsCreateAttribute().Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\3", "Models");
db.DbFirst.IsCreateAttribute().Where(it => it.ToLower().StartsWith("view")).CreateClassFile("c:\\Demo\\4", "Models");

3. Generate entities with default values

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

db.DbFirst.Where("Student")
.IsCreateAttribute()
.CreatedReplaceClassString(it = > it. Replace (" XXX, "" yyy")) / / can also use regular Regex. Replace
.CreateClassFile("c:\\Demo\\2", "Models");

4. Customize the formatting function

Add a SettingPropertyTemplate overload 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

5. Format the class name and perperty 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");

6. Replace the generated Class string

This replacement consumes the most performance and can be replaced with other functions before other functions

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

8. Add a tenant

db.DbFirst.Where("order").SettingClassDescriptionTemplate(it => {
return it+"\r\n    [Tenant(\""+db.CurrentConnectionConfig.ConfigId+"\")]";
}).CreateClassFile("c:\\Demo\\1", "Models");