Code First

This commit is contained in:
sunkaixuan
2017-06-17 20:28:15 +08:00
parent 4f8aa12745
commit 4a1172f4d6
10 changed files with 42 additions and 13 deletions

View File

@@ -10,8 +10,16 @@ namespace OrmTest.Demo
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[SugarColumn(Length=40)]
[SugarColumn(Length = 40)]
public string Name { get; set; }
[SugarColumn(IsNullable = true)]
public bool IsOk { get; set; }
public Guid Guid { get; set; }
public decimal Decimal { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime? DateTime { get; set; }
[SugarColumn(IsNullable = true)]
public double? Dob { get; set; }
}
public class CodeFirst : DemoBase
{
@@ -24,7 +32,9 @@ namespace OrmTest.Demo
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
if (db.DbMaintenance.IsAnyTable("CodeTable"))
db.DbMaintenance.DropTable("CodeTable");
db.DbMaintenance.BackupDataBase("SqlSugar4XTest","c:\\back\\1.bak");
db.CodeFirst.InitTables(typeof(CodeTable));
}
}

View File

@@ -98,13 +98,13 @@ namespace SqlSugar
DbColumnInfo dbColumnInfo = new DbColumnInfo()
{
Length = item.Length,
DataType = this.Context.Ado.DbBind.GetDbTypeName(item.PropertyInfo.PropertyType.Name),
DataType = this.Context.Ado.DbBind.GetDbTypeName(PubMethod.GetUnderType(item.PropertyInfo).Name),
TableId = entityInfo.Columns.IndexOf(item),
DbColumnName = item.DbColumnName.IsValuable() ? item.DbColumnName : item.PropertyName,
IsPrimarykey = item.IsPrimarykey,
IsIdentity = item.IsIdentity,
TableName = tableName,
IsNullable = PubMethod.IsNullable(item.PropertyInfo),
IsNullable = item.IsNullable,
DefaultValue = item.DefaultValue,
ColumnDescription = item.ColumnDescription
};

View File

@@ -27,6 +27,8 @@ namespace SqlSugar
csharpTypeName = "short";
if (csharpTypeName == "Int64")
csharpTypeName = "long";
if (csharpTypeName == "Boolean")
csharpTypeName = "bool";
var mappings = this.MappingTypes.Where(it => it.Value.ToString().Equals(csharpTypeName,StringComparison.CurrentCultureIgnoreCase));
return mappings.IsValuable() ? mappings.First().Key : "varchar";
}

View File

@@ -212,7 +212,7 @@ namespace SqlSugar
{
foreach (var item in classStringList)
{
FileHeper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("\\{0}.cs", item.Key), item.Value, Encoding.UTF8);
FileHelper.CreateFile(directoryPath.TrimEnd('\\').TrimEnd('/') + string.Format("\\{0}.cs", item.Key), item.Value, Encoding.UTF8);
}
}
}

View File

@@ -116,16 +116,24 @@ namespace SqlSugar
this.Context.Ado.ExecuteCommand(sql);
return true;
}
public bool DropTable(string tableName) {
this.Context.Ado.ExecuteCommand(string.Format(this.DropTableSql,tableName));
return true;
}
public virtual bool TruncateTable(string tableName)
{
this.Context.Ado.ExecuteCommand(string.Format(this.TruncateTableSql, tableName));
return true;
}
public bool BackupDataBase()
public bool BackupDataBase(string databaseName, string fullFileName)
{
throw new NotImplementedException();
var directory=FileHelper.GetDirectoryFromFilePath(fullFileName);
if (!FileHelper.IsExistDirectory(directory)) {
FileHelper.CreateDirectory(directory);
}
this.Context.Ado.ExecuteCommand(string.Format(this.BackupDataBaseSql, databaseName, fullFileName));
return true;
}
#endregion

View File

@@ -25,6 +25,7 @@ namespace SqlSugar
protected abstract string CreateTableColumn { get; }
protected abstract string BackupTableSql { get; }
protected abstract string TruncateTableSql { get; }
protected abstract string DropTableSql { get; }
#endregion
#region Scattered

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace SqlSugar
{
internal class FileHeper
internal class FileHelper
{
public static void CreateFile(string filePath, string text, Encoding encoding)
{

View File

@@ -32,13 +32,14 @@ namespace SqlSugar
#endregion
#region DDL
bool DropTable(string tableName);
bool TruncateTable(string tableName);
bool CreateTable(string tableName, List<DbColumnInfo> columns);
bool AddColumnToTable(string tableName, DbColumnInfo column);
bool BackupDataBase();
bool BackupDataBase(string databaseName,string fullFileName);
#endregion
}
}

View File

@@ -82,7 +82,8 @@ namespace SqlSugar
return "ALERT TABLE {0} ADD {1} {2} {3}";
}
}
protected override string AlterColumnToTableSql {
protected override string AlterColumnToTableSql
{
get
{
return "ALERT TABLE {0} ALTER COLUMN {1}{2} {3} ";
@@ -123,7 +124,13 @@ namespace SqlSugar
return "SELECT {0} * INTO {1} FROM {2}";
}
}
protected override string DropTableSql
{
get
{
return "DROP TABLE {0}";
}
}
#endregion

View File

@@ -69,7 +69,7 @@
<Compile Include="Entities\SqlFilter.cs" />
<Compile Include="Enum\DbObjectType.cs" />
<Compile Include="Enum\ProperyType.cs" />
<Compile Include="ExpressionsToSql\Common\FileHeper.cs" />
<Compile Include="Common\FileHelper.cs" />
<Compile Include="Interface\ICacheManager.cs" />
<Compile Include="Entities\DbResult.cs" />
<Compile Include="Enum\InitKeyType.cs" />