This commit is contained in:
sunkaixuan
2017-05-29 13:30:22 +08:00
parent 29821631b2
commit 8bd7b946e3
2 changed files with 61 additions and 32 deletions

View File

@@ -153,12 +153,13 @@ namespace SqlSugar
{
foreach (var item in columns)
{
var isLast = columns.Last() == item;
string PropertyText = DbFirstTemplate.PropertyTemplate;
string PropertyDescriptionText = DbFirstTemplate.PropertyDescriptionTemplate;
string SugarColumnText = DbFirstTemplate.ValueSugarCoulmn;
var hasSugarColumn = item.IsPrimarykey == true || item.IsIdentity == true||item.DbColumnName.IsValuable();
PropertyText = PropertyText.Replace(DbFirstTemplate.KeyPropertyType,item.DataType);
PropertyText = PropertyText.Replace(DbFirstTemplate.KeyPropertyName, item.DbColumnName);
PropertyText = GetPropertyText(item, PropertyText);
PropertyDescriptionText = GetPropertyDescriptionText(item, PropertyDescriptionText);
PropertyText = PropertyDescriptionText + PropertyText;
classText = classText.Replace(DbFirstTemplate.KeyPropertyName, PropertyText + (isLast?"":("\r\n" + DbFirstTemplate.KeyPropertyName)));
}
}
result.Add(className, classText);
@@ -166,6 +167,7 @@ namespace SqlSugar
}
return result;
}
public void CreateClassFile(string directoryPath, string nameSpace = "Models")
{
Check.ArgumentNullException(directoryPath, "directoryPath can't null");
@@ -180,6 +182,29 @@ namespace SqlSugar
}
#region Private methods
private string GetPropertyText(DbColumnInfo item, string PropertyText)
{
string SugarColumnText = DbFirstTemplate.ValueSugarCoulmn;
var hasSugarColumn = item.IsPrimarykey == true || item.IsIdentity == true || item.DbColumnName.IsValuable();
if (hasSugarColumn&&this.IsAttribute)
{
}
else
{
SugarColumnText = null;
}
PropertyText = PropertyText.Replace(DbFirstTemplate.KeySugarColumn, SugarColumnText);
PropertyText = PropertyText.Replace(DbFirstTemplate.KeyPropertyType, this.Context.Ado.DbBind.ChangeDBTypeToCSharpType(item.DataType));
PropertyText = PropertyText.Replace(DbFirstTemplate.KeyPropertyName, item.DbColumnName);
return PropertyText;
}
private string GetPropertyDescriptionText(DbColumnInfo item, string propertyDescriptionText)
{
propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyPropertyDescription, item.ColumnDescription);
propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyDefaultValue, item.Value.ObjToString());
propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyIsNullable, item.IsNullable.ObjToString());
return propertyDescriptionText;
}
#endregion
}

View File

@@ -8,37 +8,38 @@ namespace SqlSugar
public class DbFirstTemplate
{
#region Template
public static string ClassTemplate = @"{using}
namespace {Namespace}
{
{ClassDescription}{SugarTable}
public class {ClassName}
{
public {ClassName}(){
{Constructor}
}
{PropertyDescription}
{Property}
}
}";
public static string ClassDescriptionTemplate = @" /// <summary>
///{ClassDescription}
/// </summary>\r\n";
public static string ClassTemplate = "{using}\r\n" +
"namespace {Namespace}\r\n" +
"{\r\n" +
"{ClassDescription}\r\n{SugarTable}\r\n" +
ClassSpace+"public class {ClassName}\r\n" +
ClassSpace + "{\r\n" +
PropertySpace + "public {ClassName}(){\r\n" +
"{Constructor}\r\n" +
PropertySpace + "}\r\n" +
"{PropertyName}\r\n" +
ClassSpace + "}\r\n" +
"}\r\n";
public static string ClassDescriptionTemplate =
ClassSpace + "///<summary>\r\n" +
ClassSpace + "///{ClassDescription}" +
ClassSpace + "/// </summary>\r\n";
public static string PropertyTemplate = @" {SugarColumn}
public {PropertyType} {PropertyName} {get;set;}\r\n";
public static string PropertyTemplate = PropertySpace + "{SugarColumn}\r\n" +
PropertySpace + "public {PropertyType} {PropertyName} {get;set;}\r\n";
public static string PropertyDescriptionTemplate = @"/// <summary>
/// Desc:{PropertyDescription}
/// Default:{DefaultValue}
/// Nullable:{IsNullable}
/// </summary>\r\n";
public static string PropertyDescriptionTemplate =
PropertySpace + "/// <summary>\r\n" +
PropertySpace + "/// Desc:{PropertyDescription}\r\n" +
PropertySpace + "/// Default:{DefaultValue}\r\n" +
PropertySpace + "/// Nullable:{IsNullable}\r\n" +
PropertySpace + "/// </summary>";
public static string ConstructorTemplate = @" this.{$PropertyName} =Convert.To{PropertyType}(""{DefaultValue}"");\r\n";
public static string ConstructorTemplate = PropertySpace + "this.{$PropertyName} =Convert.To{PropertyType}(\"{DefaultValue}\");\r\n";
public static string UsingTemplate = @"using System;
using System.Linq;
using System.Text;"+"\r\n";
public static string UsingTemplate = "using System;\r\n" +
"using System.Linq;\r\n" +
"using System.Text;" + "\r\n";
#endregion
#region Replace Key
@@ -58,7 +59,10 @@ using System.Text;"+"\r\n";
#region Replace Value
public const string ValueSugarTable = "[SugarTable(\"{0}\")]";
public const string ValueSugarCoulmn= "[SugarColumn({0})]";
public const string ValueSugarCoulmn = "[SugarColumn({0})]";
#endregion
public const string PropertySpace = " ";
public const string ClassSpace = " ";
}
}