From 4f8aa12745e29c25ad7c0fd214f3f675cbe12ef2 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 17 Jun 2017 19:28:30 +0800 Subject: [PATCH] Code First --- SqlServerTest/Demos/5_CodeFirst.cs | 31 ++++++++++++ SqlServerTest/Program.cs | 49 ++++++++++--------- SqlServerTest/SqlServerTest.csproj | 1 + .../CodeFirstProvider/CodeFirstProvider.cs | 1 + .../Abstract/DbBindProvider/DbBindProvider.cs | 8 ++- .../Abstract/DbMaintenanceProvider/Methods.cs | 24 ++++++++- .../DbMaintenanceProvider/Properties.cs | 9 +++- .../Abstract/EntityProvider/EntityProvider.cs | 2 + .../Entities/Mapping/SugarMappingAttribute.cs | 14 ++++++ SqlSugar/Interface/IDbMaintenance.cs | 4 ++ .../SqlServer/DbBind/SqlServerDbBind.cs | 6 +-- .../DbMaintenance/SqlServerDbMaintenance.cs | 37 +++++++++++++- 12 files changed, 155 insertions(+), 31 deletions(-) create mode 100644 SqlServerTest/Demos/5_CodeFirst.cs diff --git a/SqlServerTest/Demos/5_CodeFirst.cs b/SqlServerTest/Demos/5_CodeFirst.cs new file mode 100644 index 000000000..e103fbf4b --- /dev/null +++ b/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)); + } + } +} diff --git a/SqlServerTest/Program.cs b/SqlServerTest/Program.cs index caafd709e..abf69ec68 100644 --- a/SqlServerTest/Program.cs +++ b/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(); } } } diff --git a/SqlServerTest/SqlServerTest.csproj b/SqlServerTest/SqlServerTest.csproj index 4f1bfce69..d9333ea59 100644 --- a/SqlServerTest/SqlServerTest.csproj +++ b/SqlServerTest/SqlServerTest.csproj @@ -48,6 +48,7 @@ + diff --git a/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index 24d3fed60..a638e4d1e 100644 --- a/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/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); diff --git a/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs b/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs index 4bc61dcca..f3ff1aae7 100644 --- a/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs +++ b/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) diff --git a/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index a6222e420..76f2ba4ff 100644 --- a/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -83,6 +83,27 @@ namespace SqlSugar } #endregion + #region Get Sql + public virtual string GetCreateTableSql(string tableName, List columns) + { + List columnArray = new List(); + 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 columns) { - this.Context.Ado.ExecuteCommand(this.CreateTableSql); + string sql = GetCreateTableSql(tableName, columns); + this.Context.Ado.ExecuteCommand(sql); return true; } diff --git a/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs b/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs index c5f11c7c1..a7e47346c 100644 --- a/SqlSugar/Abstract/DbMaintenanceProvider/Properties.cs +++ b/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 } } diff --git a/SqlSugar/Abstract/EntityProvider/EntityProvider.cs b/SqlSugar/Abstract/EntityProvider/EntityProvider.cs index a0ccff630..8dbdbf22e 100644 --- a/SqlSugar/Abstract/EntityProvider/EntityProvider.cs +++ b/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 { diff --git a/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs b/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs index b4ed0e268..ca94bb815 100644 --- a/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs +++ b/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; } + } } } diff --git a/SqlSugar/Interface/IDbMaintenance.cs b/SqlSugar/Interface/IDbMaintenance.cs index dccc2feb7..f0794239b 100644 --- a/SqlSugar/Interface/IDbMaintenance.cs +++ b/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 columns); + #endregion + #region DDL bool TruncateTable(string tableName); diff --git a/SqlSugar/Realization/SqlServer/DbBind/SqlServerDbBind.cs b/SqlSugar/Realization/SqlServer/DbBind/SqlServerDbBind.cs index 7f3920e8c..5a7f24c33 100644 --- a/SqlSugar/Realization/SqlServer/DbBind/SqlServerDbBind.cs +++ b/SqlSugar/Realization/SqlServer/DbBind/SqlServerDbBind.cs @@ -13,23 +13,23 @@ namespace SqlSugar return new List>() { new KeyValuePair("int",CSharpDataType.@int), + new KeyValuePair("nvarchar",CSharpDataType.@string), new KeyValuePair("sql_variant",CSharpDataType.@string), new KeyValuePair("text",CSharpDataType.@string), new KeyValuePair("char",CSharpDataType.@string), new KeyValuePair("ntext",CSharpDataType.@string), new KeyValuePair("nchar",CSharpDataType.@string), new KeyValuePair("varchar",CSharpDataType.@string), - new KeyValuePair("nvarchar",CSharpDataType.@string), new KeyValuePair("bigint",CSharpDataType.@long), new KeyValuePair("bit",CSharpDataType.@bool), - new KeyValuePair("time",CSharpDataType.DateTime), new KeyValuePair("datetime",CSharpDataType.DateTime), + new KeyValuePair("time",CSharpDataType.DateTime), new KeyValuePair("smalldatetime",CSharpDataType.DateTime), new KeyValuePair("timestamp",CSharpDataType.DateTime), new KeyValuePair("datetime2",CSharpDataType.DateTime), new KeyValuePair("date",CSharpDataType.DateTime), - new KeyValuePair("single",CSharpDataType.@decimal), new KeyValuePair("decimal",CSharpDataType.@decimal), + new KeyValuePair("single",CSharpDataType.@decimal), new KeyValuePair("money",CSharpDataType.@decimal), new KeyValuePair("numeric",CSharpDataType.@decimal), new KeyValuePair("smallmoney",CSharpDataType.@decimal), diff --git a/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs b/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs index 0caa30349..52af8e5d1 100644 --- a/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs +++ b/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 } }