Code First

This commit is contained in:
sunkaixuan
2017-06-17 22:07:51 +08:00
parent 58d5e9e8d2
commit d8b24344e7
4 changed files with 20 additions and 29 deletions

View File

@@ -10,7 +10,7 @@ namespace OrmTest.Demo
{ {
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; } public int Id { get; set; }
[SugarColumn(Length = 40)] [SugarColumn(Length = 100)]
public string Name { get; set; } public string Name { get; set; }
[SugarColumn(IsNullable = true)] [SugarColumn(IsNullable = true)]
public bool IsOk { get; set; } public bool IsOk { get; set; }

View File

@@ -122,6 +122,7 @@ namespace SqlSugar
} }
#endregion #endregion
#region Core
public Dictionary<string, string> ToClassStringList(string nameSpace = "Models") public Dictionary<string, string> ToClassStringList(string nameSpace = "Models")
{ {
this.Namespace = nameSpace; this.Namespace = nameSpace;
@@ -183,7 +184,21 @@ namespace SqlSugar
} }
return result; return result;
} }
public void CreateClassFile(string directoryPath, string nameSpace = "Models")
{
Check.ArgumentNullException(directoryPath, "directoryPath can't null");
var classStringList = ToClassStringList(nameSpace);
if (classStringList.IsValuable())
{
foreach (var item in classStringList)
{
FileHelper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("\\{0}.cs", item.Key), item.Value, Encoding.UTF8);
}
}
}
#endregion
#region Private methods
private string GetProertypeDefaultValue(DbColumnInfo item) private string GetProertypeDefaultValue(DbColumnInfo item)
{ {
var result = item.DefaultValue; var result = item.DefaultValue;
@@ -203,21 +218,6 @@ namespace SqlSugar
result = result.Replace("\r", "\t").Replace("\n", "\t"); result = result.Replace("\r", "\t").Replace("\n", "\t");
return result; return result;
} }
public void CreateClassFile(string directoryPath, string nameSpace = "Models")
{
Check.ArgumentNullException(directoryPath, "directoryPath can't null");
var classStringList = ToClassStringList(nameSpace);
if (classStringList.IsValuable())
{
foreach (var item in classStringList)
{
FileHelper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("\\{0}.cs", item.Key), item.Value, Encoding.UTF8);
}
}
}
#region Private methods
private string GetPropertyText(DbColumnInfo item, string PropertyText) private string GetPropertyText(DbColumnInfo item, string PropertyText)
{ {
string SugarColumnText = DbFirstTemplate.ValueSugarCoulmn; string SugarColumnText = DbFirstTemplate.ValueSugarCoulmn;
@@ -266,7 +266,6 @@ namespace SqlSugar
return item.DbColumnName; return item.DbColumnName;
} }
} }
private string GetPropertyTypeName(DbColumnInfo item) private string GetPropertyTypeName(DbColumnInfo item)
{ {
string result = this.Context.Ado.DbBind.GetPropertyTypeName(item.DataType); string result = this.Context.Ado.DbBind.GetPropertyTypeName(item.DataType);
@@ -283,7 +282,6 @@ namespace SqlSugar
string result = this.Context.Ado.DbBind.GetConvertString(item.DataType) + "(\"" + convertString + "\")"; string result = this.Context.Ado.DbBind.GetConvertString(item.DataType) + "(\"" + convertString + "\")";
return result; return result;
} }
private string GetPropertyDescriptionText(DbColumnInfo item, string propertyDescriptionText) private string GetPropertyDescriptionText(DbColumnInfo item, string propertyDescriptionText)
{ {
propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyPropertyDescription, item.ColumnDescription); propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyPropertyDescription, item.ColumnDescription);
@@ -291,7 +289,6 @@ namespace SqlSugar
propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyIsNullable, item.IsNullable.ObjToString()); propertyDescriptionText = propertyDescriptionText.Replace(DbFirstTemplate.KeyIsNullable, item.IsNullable.ObjToString());
return propertyDescriptionText; return propertyDescriptionText;
} }
#endregion #endregion
} }
} }

View File

@@ -10,13 +10,9 @@ namespace SqlSugar
#region DML #region DML
List<DbTableInfo> GetViewInfoList(); List<DbTableInfo> GetViewInfoList();
List<DbTableInfo> GetTableInfoList(); List<DbTableInfo> GetTableInfoList();
List<DbColumnInfo> GetColumnInfosByTableName(string tableName); List<DbColumnInfo> GetColumnInfosByTableName(string tableName);
List<string> GetIsIdentities(string tableName); List<string> GetIsIdentities(string tableName);
List<string> GetPrimaries(string tableName); List<string> GetPrimaries(string tableName);
#endregion #endregion
@@ -30,11 +26,9 @@ namespace SqlSugar
#region DDL #region DDL
bool DropTable(string tableName); bool DropTable(string tableName);
bool TruncateTable(string tableName); bool TruncateTable(string tableName);
bool CreateTable(string tableName, List<DbColumnInfo> columns); bool CreateTable(string tableName, List<DbColumnInfo> columns);
bool AddColumnToTable(string tableName, DbColumnInfo column); bool AddColumnToTable(string tableName, DbColumnInfo column);
bool AddKey(string tableName,string columnName,bool isPrimaryKey, bool isIdentity);
bool BackupDataBase(string databaseName,string fullFileName); bool BackupDataBase(string databaseName,string fullFileName);
bool DropColumn(string tableName,string columnName); bool DropColumn(string tableName,string columnName);
#endregion #endregion

View File

@@ -79,14 +79,14 @@ namespace SqlSugar
{ {
get get
{ {
return "ALERT TABLE {0} ADD {1} {2}{3} {4} {5} {6}"; return "ALTER TABLE {0} ADD {1} {2}{3} {4} {5} {6}";
} }
} }
protected override string AlterColumnToTableSql protected override string AlterColumnToTableSql
{ {
get get
{ {
return "ALERT TABLE {0} ALTER COLUMN {1}{2} {3} "; return "ALTER TABLE {0} ALTER COLUMN {1}{2} {3} ";
} }
} }
protected override string BackupDataBaseSql protected override string BackupDataBaseSql