diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs index 845a454a8..9d77ee66a 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbMaintenanceProvider/Methods.cs @@ -192,7 +192,7 @@ namespace SqlSugar { oldTableName = this.SqlBuilder.GetTranslationTableName(oldTableName); newTableName = this.SqlBuilder.GetTranslationTableName(newTableName); - string sql = string.Format(this.BackupTableSql, maxBackupDataRows,newTableName , oldTableName); + string sql = string.Format(this.BackupTableSql,newTableName , oldTableName, maxBackupDataRows); this.Context.Ado.ExecuteCommand(sql); return true; } diff --git a/Src/Asp.Net/SqlSugar/Realization/Sqlite/CodeFirst/SqliteCodeFirst.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/CodeFirst/SqliteCodeFirst.cs index 74c5aa1f8..e9919bf3e 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Sqlite/CodeFirst/SqliteCodeFirst.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Sqlite/CodeFirst/SqliteCodeFirst.cs @@ -7,9 +7,10 @@ namespace SqlSugar { public class SqliteCodeFirst : CodeFirstProvider { - public override void NoExistLogic(EntityInfo entityInfo) + public override void ExistLogic(EntityInfo entityInfo) { var tableName = GetTableName(entityInfo); + string backupName = tableName + DateTime.Now.ToString("yyyyMMddHHmmss"); Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1"); List columns = new List(); if (entityInfo.Columns.IsValuable()) @@ -20,6 +21,24 @@ namespace SqlSugar columns.Add(dbColumnInfo); } } + this.Context.DbMaintenance.BackupTable(tableName, backupName, int.MaxValue); + this.Context.DbMaintenance.DropTable(tableName); + this.Context.DbMaintenance.CreateTable(tableName,columns); + } + public override void NoExistLogic(EntityInfo entityInfo) + { + var tableName = GetTableName(entityInfo); + string backupName=tableName+DateTime.Now.ToString("yyyyMMddHHmmss"); + Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1"); + List columns = new List(); + if (entityInfo.Columns.IsValuable()) + { + foreach (var item in entityInfo.Columns.Where(it=>it.IsIgnore==false)) + { + DbColumnInfo dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item); + columns.Add(dbColumnInfo); + } + } this.Context.DbMaintenance.CreateTable(tableName, columns); } protected override DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item) diff --git a/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbBind/SqliteDbBind.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbBind/SqliteDbBind.cs index df3183b35..c2a13e82a 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbBind/SqliteDbBind.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbBind/SqliteDbBind.cs @@ -30,8 +30,8 @@ namespace SqlSugar return new List>() { - new KeyValuePair("int",CSharpDataType.@int), new KeyValuePair("integer",CSharpDataType.@int), + new KeyValuePair("int",CSharpDataType.@int), new KeyValuePair("int32",CSharpDataType.@int), new KeyValuePair("integer32",CSharpDataType.@int), diff --git a/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs index abe7c3011..9a28732f1 100644 --- a/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs @@ -89,7 +89,7 @@ namespace SqlSugar { get { - throw new NotSupportedException(); + return " CREATE TABLE {0} AS SELECT * FROM {1} limit 0,{2}"; } } protected override string DropTableSql @@ -110,7 +110,7 @@ namespace SqlSugar { get { - return "ALTER TABLE {0} drop primary key;"; + throw new NotSupportedException(); } } protected override string RenameColumnSql @@ -182,7 +182,7 @@ namespace SqlSugar this.Context.Ado.IsEnableLogEvent = oldIsEnableLog; return AdoCore.GetColumnInfosByTableName(tableName, reader); } - + }); } @@ -194,18 +194,16 @@ namespace SqlSugar { if (item.DbColumnName.Equals("GUID", StringComparison.CurrentCultureIgnoreCase)) { - item.Length = 10; + item.Length = 20; + } + if (item.IsIdentity && !item.IsPrimarykey) + { + item.IsPrimarykey = true; + Check.Exception(item.DataType == "integer", "Identity only integer type"); } } } string sql = GetCreateTableSql(tableName, columns); - string primaryKeyInfo = null; - if (columns.Any(it => it.IsIdentity)) - { - primaryKeyInfo = string.Format(", Primary key({0})", string.Join(",", columns.Where(it => it.IsIdentity).Select(it => this.SqlBuilder.GetTranslationColumnName(it.DbColumnName)))); - - } - sql = sql.Replace("$PrimaryKey", primaryKeyInfo); this.Context.Ado.ExecuteCommand(sql); return true; } @@ -223,12 +221,13 @@ namespace SqlSugar } string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null; string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull; - string primaryKey = null; + string primaryKey = item.IsPrimarykey?this.CreateTablePirmaryKey:null; string identity = item.IsIdentity ? this.CreateTableIdentity : null; string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity); columnArray.Add(addItem); } string tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray)); + tableString = tableString.Replace("`", "\""); return tableString; } public override bool IsAnyConstraint(string constraintName) diff --git a/Src/Asp.Net/SqliteTest/Config.cs b/Src/Asp.Net/SqliteTest/Config.cs index f9eff003e..a9f84d953 100644 --- a/Src/Asp.Net/SqliteTest/Config.cs +++ b/Src/Asp.Net/SqliteTest/Config.cs @@ -8,6 +8,6 @@ namespace OrmTest { public class Config { - public static string ConnectionString = "DataSource=" + System.AppDomain.CurrentDomain.BaseDirectory + "DataBase\\SqlSugar4xTest.sqlite"; + public static string ConnectionString = "DataSource=F:\\MyOpenSource\\SqlSugar4.XNew\\SqlSugar\\Src\\Asp.Net\\SqliteTest\\DataBase\\\\SqlSugar4xTest.sqlite"; } } diff --git a/Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite b/Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite index 303fb68a9..9e1add14f 100644 Binary files a/Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite and b/Src/Asp.Net/SqliteTest/DataBase/SqlSugar4xTest.sqlite differ diff --git a/Src/Asp.Net/SqliteTest/DataBase/X.sql b/Src/Asp.Net/SqliteTest/DataBase/X.sql new file mode 100644 index 000000000..6e1193334 --- /dev/null +++ b/Src/Asp.Net/SqliteTest/DataBase/X.sql @@ -0,0 +1,24 @@ +/* +Navicat SQLite Data Transfer + +Source Server : x +Source Server Version : 30714 +Source Host : :0 + +Target Server Type : SQLite +Target Server Version : 30714 +File Encoding : 65001 + +Date: 2017-07-09 17:02:41 +*/ + +PRAGMA foreign_keys = OFF; + +-- ---------------------------- +-- Table structure for X +-- ---------------------------- +DROP TABLE IF EXISTS "main"."X"; +CREATE TABLE "X" ( +"T" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, +"x" time +); diff --git a/Src/Asp.Net/SqliteTest/Program.cs b/Src/Asp.Net/SqliteTest/Program.cs index 50ccdd40b..bc5007cc9 100644 --- a/Src/Asp.Net/SqliteTest/Program.cs +++ b/Src/Asp.Net/SqliteTest/Program.cs @@ -39,11 +39,11 @@ namespace OrmTest //OrmTest.Demo.Insert.Init(); //OrmTest.Demo.Delete.Init(); //OrmTest.Demo.Update.Init(); - OrmTest.Demo.DbFirst.Init(); + //OrmTest.Demo.DbFirst.Init(); //OrmTest.Demo.JoinSql.Init(); //OrmTest.Demo.Filter.Init(); //OrmTest.Demo.ComplexModel.Init(); - //OrmTest.Demo.CodeFirst.Init(); + OrmTest.Demo.CodeFirst.Init(); } } } diff --git a/Src/Asp.Net/SqliteTest/SqliteTest.csproj b/Src/Asp.Net/SqliteTest/SqliteTest.csproj index 478f4d1b6..37805c632 100644 --- a/Src/Asp.Net/SqliteTest/SqliteTest.csproj +++ b/Src/Asp.Net/SqliteTest/SqliteTest.csproj @@ -91,9 +91,7 @@ - - Always - +