This commit is contained in:
sunkaixuan 2017-05-29 21:24:27 +08:00
parent bd3bcc0e1b
commit aab31c4af2
3 changed files with 44 additions and 11 deletions

View File

@ -6,7 +6,7 @@ using System.Text;
namespace OrmTest.Demo
{
public class DbFirst:DemoBase
public class DbFirst : DemoBase
{
public static void Init()
{
@ -27,13 +27,46 @@ namespace OrmTest.Demo
db.MappingTables.Clear();
//Create class with default value
db.DbFirst.IsCreateDefaultValue().CreateClassFile("c:\\Demo\\4","Demo.Models");
db.DbFirst.IsCreateDefaultValue().CreateClassFile("c:\\Demo\\4", "Demo.Models");
//Mapping and Attribute
db.MappingTables.Add("ClassStudent", "Student");
db.MappingColumns.Add("NewId", "Id", "ClassStudent");
db.DbFirst.IsCreateAttribute().Where("Student").CreateClassFile("c:\\Demo\\5");
//Remove mapping
db.MappingTables.Clear();
db.MappingColumns.Clear();
//Custom format,Change old to new
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\\6");
}
}
}

View File

@ -34,10 +34,10 @@ namespace OrmTest
//new SqlSugarPerformance(100).Select();
/***Demo***/
OrmTest.Demo.Query.Init();
OrmTest.Demo.Insert.Init();
OrmTest.Demo.Delete.Init();
OrmTest.Demo.Update.Init();
//OrmTest.Demo.Query.Init();
//OrmTest.Demo.Insert.Init();
//OrmTest.Demo.Delete.Init();
//OrmTest.Demo.Update.Init();
OrmTest.Demo.DbFirst.Init();
}
}

View File

@ -132,7 +132,7 @@ namespace SqlSugar
var columns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableInfo.Name);
string className = tableInfo.Name;
string classText = this.ClassTemplate;
string ConstructorText = IsDefaultValue ? DbFirstTemplate.ConstructorTemplate : null;
string ConstructorText = IsDefaultValue ? this.ConstructorTemplate : null;
if (this.Context.MappingTables.IsValuable())
{
var mappingInfo = this.Context.MappingTables.FirstOrDefault(it => it.DbTableName.Equals(tableInfo.Name, StringComparison.CurrentCultureIgnoreCase));
@ -148,15 +148,15 @@ namespace SqlSugar
classText = classText.Replace(DbFirstTemplate.KeyClassName, className);
classText = classText.Replace(DbFirstTemplate.KeyNamespace, this.Namespace);
classText = classText.Replace(DbFirstTemplate.KeyUsing, IsAttribute ? (this.UsingTemplate + "using " + PubConst.AssemblyName + ";\r\n") : this.UsingTemplate);
classText = classText.Replace(DbFirstTemplate.KeyClassDescription, DbFirstTemplate.ClassDescriptionTemplate.Replace(DbFirstTemplate.KeyClassDescription, tableInfo.Description + "\r\n"));
classText = classText.Replace(DbFirstTemplate.KeyClassDescription, this.ClassDescriptionTemplate.Replace(DbFirstTemplate.KeyClassDescription, tableInfo.Description + "\r\n"));
classText = classText.Replace(DbFirstTemplate.KeySugarTable, IsAttribute ? string.Format(DbFirstTemplate.ValueSugarTable, tableInfo.Name): null);
if (columns.IsValuable())
{
foreach (var item in columns)
{
var isLast = columns.Last() == item;
string PropertyText = DbFirstTemplate.PropertyTemplate;
string PropertyDescriptionText = DbFirstTemplate.PropertyDescriptionTemplate;
string PropertyText = this.PropertyTemplate;
string PropertyDescriptionText = this.PropertyDescriptionTemplate;
string propertyName = GetPropertyName(item);
string propertyTypeName = GetPropertyTypeName(item);
PropertyText = GetPropertyText(item, PropertyText);
@ -166,7 +166,7 @@ namespace SqlSugar
if (ConstructorText.IsValuable() && item.DefaultValue.IsValuable())
{
ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyPropertyName, propertyName);
ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyDefaultValue, GetPropertyTypeConvert(item)) + (isLast ? "" : DbFirstTemplate.ConstructorTemplate);
ConstructorText = ConstructorText.Replace(DbFirstTemplate.KeyDefaultValue, GetPropertyTypeConvert(item)) + (isLast ? "" : this.ConstructorTemplate);
}
}
}