SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.6 KiB

6 years ago
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest
{
public class DemoD_DbFirst
{
public static void Init()
{
6 years ago
Console.WriteLine();
Console.WriteLine("#### DbFirst Start ####");
6 years ago
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = DbType.SqlServer,
ConnectionString = Config.ConnectionString,
InitKeyType = InitKeyType.Attribute,
6 years ago
IsAutoCloseConnection = true
6 years ago
});
6 years ago
6 years ago
db.DbFirst.CreateClassFile("c:\\Demo\\1", "Models");
db.DbFirst.Where("Student").CreateClassFile("c:\\Demo\\2", "Models");
6 years ago
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");
6 years ago
6 years ago
db.DbFirst.IsCreateDefaultValue().CreateClassFile("c:\\Demo\\6", "Demo.Models");
6 years ago
6 years ago
db.DbFirst. SettingClassTemplate(old => { return old;})
.SettingNamespaceTemplate(old =>{ return old;})
.SettingPropertyDescriptionTemplate(old =>
{
return @" /// <summary>
/// Desc_New:{PropertyDescription}
/// Default_New:{DefaultValue}
/// Nullable_New:{IsNullable}
/// </summary>";
})
.SettingPropertyTemplate(old =>{return old;})
.SettingConstructorTemplate(old =>{return old; })
.CreateClassFile("c:\\Demo\\7");
6 years ago
3 years ago
db.DbFirst
.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\\8");
6 years ago
foreach (var item in db.DbMaintenance.GetTableInfoList())
{
string entityName = item.Name.ToUpper();/*Format class name*/
db.MappingTables.Add(entityName , item.Name);
foreach (var col in db.DbMaintenance.GetColumnInfosByTableName(item.Name))
{
db.MappingColumns.Add(col.DbColumnName.ToUpper() /*Format class property name*/, col.DbColumnName, entityName);
}
}
3 years ago
db.DbFirst.IsCreateAttribute().CreateClassFile("c:\\Demo\\9", "Models");
6 years ago
6 years ago
//Use Razor Template
//db.DbFirst.UseRazorAnalysis(RazorFirst.DefaultRazorClassTemplate).CreateClassFile("");
6 years ago
Console.WriteLine("#### DbFirst End ####");
6 years ago
}
}
}