Browse Source

Code First

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
4f8aa12745
  1. 31
      SqlServerTest/Demos/5_CodeFirst.cs
  2. 49
      SqlServerTest/Program.cs
  3. 1
      SqlServerTest/SqlServerTest.csproj
  4. 1
      SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
  5. 8
      SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs
  6. 24
      SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs
  7. 9
      SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs
  8. 2
      SqlSugar/Abstract/EntityProvider/EntityProvider.cs
  9. 14
      SqlSugar/Entities/Mapping/SugarMappingAttribute.cs
  10. 4
      SqlSugar/Interface/IDbMaintenance.cs
  11. 6
      SqlSugar/Realization/SqlServer/DbBind/SqlServerDbBind.cs
  12. 37
      SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs

31
SqlServerTest/Demos/5_CodeFirst.cs

@ -0,0 +1,31 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
public class CodeTable
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[SugarColumn(Length=40)]
public string Name { get; set; }
}
public class CodeFirst : DemoBase
{
public static void Init()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
db.CodeFirst.InitTables(typeof(CodeTable));
}
}
}

49
SqlServerTest/Program.cs

@ -18,31 +18,32 @@ namespace OrmTest
static void Main(string[] args)
{
/***Unit Test***/
new Field(1).Init();
new Where(1).Init();
new Method(1).Init();
new JoinQuery(1).Init();
new SingleQuery(1).Init();
new SelectQuery(1).Init();
new AutoClose(1).Init();
new Insert(1).Init();
new Delete(1).Init();
new Update(1).Init();
new Mapping(1).Init();
new DataTest(1).Init();
/***Performance Test***/
new SqlSugarPerformance(100).Select();
//new Field(1).Init();
//new Where(1).Init();
//new Method(1).Init();
//new JoinQuery(1).Init();
//new SingleQuery(1).Init();
//new SelectQuery(1).Init();
//new AutoClose(1).Init();
//new Insert(1).Init();
//new Delete(1).Init();
//new Update(1).Init();
//new Mapping(1).Init();
//new DataTest(1).Init();
///***Performance Test***/
//new SqlSugarPerformance(100).Select();
/***Demo***/
OrmTest.Demo.Query.Init();
OrmTest.Demo.Insert.Init();
OrmTest.Demo.Delete.Init();
OrmTest.Demo.Update.Init();
OrmTest.Demo.DbFirst.Init();
OrmTest.Demo.JoinSql.Init();
OrmTest.Demo.Filter.Init();
OrmTest.Demo.MaterSlave.Init();
OrmTest.Demo.ComplexModel.Init();
///***Demo***/
//OrmTest.Demo.Query.Init();
//OrmTest.Demo.Insert.Init();
//OrmTest.Demo.Delete.Init();
//OrmTest.Demo.Update.Init();
//OrmTest.Demo.DbFirst.Init();
//OrmTest.Demo.JoinSql.Init();
//OrmTest.Demo.Filter.Init();
//OrmTest.Demo.MaterSlave.Init();
//OrmTest.Demo.ComplexModel.Init();
OrmTest.Demo.CodeFirst.Init();
}
}
}

1
SqlServerTest/SqlServerTest.csproj

@ -48,6 +48,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="Demos\5_CodeFirst.cs" />
<Compile Include="Demos\6_ComplexModel.cs" />
<Compile Include="Demos\7_Filter.cs" />
<Compile Include="Demos\8_JoinSql.cs" />

1
SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs

@ -108,6 +108,7 @@ namespace SqlSugar
DefaultValue = item.DefaultValue,
ColumnDescription = item.ColumnDescription
};
columns.Add(dbColumnInfo);
}
}
this.Context.DbMaintenance.CreateTable(tableName, columns);

8
SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs

@ -21,7 +21,13 @@ namespace SqlSugar
{
return "varbinary";
}
var mappings = this.MappingTypes.Where(it => it.Value.ToString() == csharpTypeName);
if (csharpTypeName == "Int32")
csharpTypeName = "int";
if (csharpTypeName == "Int16")
csharpTypeName = "short";
if (csharpTypeName == "Int64")
csharpTypeName = "long";
var mappings = this.MappingTypes.Where(it => it.Value.ToString().Equals(csharpTypeName,StringComparison.CurrentCultureIgnoreCase));
return mappings.IsValuable() ? mappings.First().Key : "varchar";
}
public string GetCsharpTypeName(string dbTypeName)

24
SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs

@ -83,6 +83,27 @@ namespace SqlSugar
}
#endregion
#region Get Sql
public virtual string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
{
List<string> columnArray = new List<string>();
Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
foreach (var item in columns)
{
string columnName = item.DbColumnName;
string dataType = item.DataType;
string dataSize = item.Length > 0 ? string.Format("({0})",item.Length) : null;
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
string primaryKey =item.IsPrimarykey? this.CreateTablePirmaryKey:null;
string identity = item.IsIdentity? this.CreateTableIdentity:null;
string addItem= string.Format(this.CreateTableColumn, columnName, dataType, dataSize, nullType, primaryKey,identity);
columnArray.Add(addItem);
}
string tableString = string.Format(this.CreateTableSql,tableName, string.Join(",\r\n", columnArray));
return tableString;
}
#endregion
#region DDL
public bool AddColumnToTable(string tableName, DbColumnInfo columnName)
{
@ -91,7 +112,8 @@ namespace SqlSugar
public virtual bool CreateTable(string tableName, List<DbColumnInfo> columns)
{
this.Context.Ado.ExecuteCommand(this.CreateTableSql);
string sql = GetCreateTableSql(tableName, columns);
this.Context.Ado.ExecuteCommand(sql);
return true;
}

9
SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs

@ -24,7 +24,14 @@ namespace SqlSugar
protected abstract string CreateTableSql { get; }
protected abstract string CreateTableColumn { get; }
protected abstract string BackupTableSql { get; }
protected abstract string TruncateTableSql { get; }
protected abstract string TruncateTableSql { get; }
#endregion
#region Scattered
protected abstract string CreateTableNull { get; }
protected abstract string CreateTableNotNull { get; }
protected abstract string CreateTablePirmaryKey { get; }
protected abstract string CreateTableIdentity { get; }
#endregion
}
}

2
SqlSugar/Abstract/EntityProvider/EntityProvider.cs

@ -119,6 +119,8 @@ namespace SqlSugar
column.IsPrimarykey = sugarColumn.IsPrimaryKey;
column.IsIdentity = sugarColumn.IsIdentity;
column.ColumnDescription = sugarColumn.ColumnDescription;
column.IsNullable = sugarColumn.IsNullable;
column.Length = sugarColumn.Length;
}
else
{

14
SqlSugar/Entities/Mapping/SugarMappingAttribute.cs

@ -58,6 +58,20 @@ namespace SqlSugar
get { return _ColumnDescription; }
set { _ColumnDescription = value; }
}
private int _Length;
public int Length
{
get { return _Length; }
set { _Length = value; }
}
private bool _IsNullable;
public bool IsNullable
{
get { return _IsNullable; }
set { _IsNullable = value; }
}
}
}

4
SqlSugar/Interface/IDbMaintenance.cs

@ -27,6 +27,10 @@ namespace SqlSugar
bool IsIdentity(string tableName, string column);
#endregion
#region Get Sql
string GetCreateTableSql(string tableName, List<DbColumnInfo> columns);
#endregion
#region DDL
bool TruncateTable(string tableName);

6
SqlSugar/Realization/SqlServer/DbBind/SqlServerDbBind.cs

@ -13,23 +13,23 @@ namespace SqlSugar
return new List<KeyValuePair<string, CSharpDataType>>()
{
new KeyValuePair<string, CSharpDataType>("int",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("nvarchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("sql_variant",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("text",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("ntext",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("nchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("nvarchar",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("bigint",CSharpDataType.@long),
new KeyValuePair<string, CSharpDataType>("bit",CSharpDataType.@bool),
new KeyValuePair<string, CSharpDataType>("time",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("datetime",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("time",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("smalldatetime",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("timestamp",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("datetime2",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("date",CSharpDataType.DateTime),
new KeyValuePair<string, CSharpDataType>("single",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("decimal",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("single",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("money",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("numeric",CSharpDataType.@decimal),
new KeyValuePair<string, CSharpDataType>("smallmoney",CSharpDataType.@decimal),

37
SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs

@ -106,7 +106,7 @@ namespace SqlSugar
{
get
{
return @"{0} {1}{2} {3}{4}\r\n";
return "{0} {1}{2} {3} {4} {5}";
}
}
protected override string TruncateTableSql
@ -123,6 +123,41 @@ namespace SqlSugar
return "SELECT {0} * INTO {1} FROM {2}";
}
}
#endregion
#region Scattered
protected override string CreateTableNull
{
get
{
return "NULL";
}
}
protected override string CreateTableNotNull
{
get
{
return "NOT NULL";
}
}
protected override string CreateTablePirmaryKey
{
get
{
return "PRIMARY KEY";
}
}
protected override string CreateTableIdentity
{
get
{
return "IDENTITY(1,1)";
}
}
#endregion
}
}

Loading…
Cancel
Save