mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-24 07:22:57 +08:00
Update Sqlite CodeFirst
This commit is contained in:
parent
d8d5176428
commit
929def762c
@ -9,8 +9,8 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
#region Properties
|
#region Properties
|
||||||
public virtual SqlSugarClient Context { get; set; }
|
public virtual SqlSugarClient Context { get; set; }
|
||||||
private bool IsBackupTable { get; set; }
|
protected bool IsBackupTable { get; set; }
|
||||||
private int MaxBackupDataRows { get; set; }
|
protected int MaxBackupDataRows { get; set; }
|
||||||
protected virtual int DefultLength { get; set; }
|
protected virtual int DefultLength { get; set; }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
@ -9,21 +9,83 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
public override void ExistLogic(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<DbColumnInfo> columns = new List<DbColumnInfo>();
|
|
||||||
if (entityInfo.Columns.HasValue())
|
if (entityInfo.Columns.HasValue())
|
||||||
{
|
{
|
||||||
foreach (var item in entityInfo.Columns.Where(it => it.IsIgnore == false))
|
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||||
|
|
||||||
|
var tableName = GetTableName(entityInfo);
|
||||||
|
var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName);
|
||||||
|
ConvertColumns(dbColumns);
|
||||||
|
var entityColumns = entityInfo.Columns.Where(it => it.IsIgnore == false).ToList();
|
||||||
|
var dropColumns = dbColumns
|
||||||
|
.Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
.Where(dc => !entityColumns.Any(ec => dc.DbColumnName.Equals(ec.DbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
.ToList();
|
||||||
|
var addColumns = entityColumns
|
||||||
|
.Where(ec => ec.OldDbColumnName.IsNullOrEmpty() || !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
.Where(ec => !dbColumns.Any(dc => ec.DbColumnName.Equals(dc.DbColumnName, StringComparison.CurrentCultureIgnoreCase))).ToList();
|
||||||
|
//var alterColumns = entityColumns
|
||||||
|
// .Where(ec => !dbColumns.Any(dc => dc.DbColumnName.Equals(ec.OldDbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
// .Where(ec =>
|
||||||
|
// dbColumns.Any(dc => dc.DbColumnName.Equals(ec.DbColumnName)
|
||||||
|
// && ((!UtilMethods.GetUnderType(ec.PropertyInfo).IsEnum() && UtilMethods.GetUnderType(ec.PropertyInfo).IsIn(UtilConstants.StringType)) ||
|
||||||
|
|
||||||
|
// IsSamgeType(ec, dc)))).ToList();
|
||||||
|
var renameColumns = entityColumns
|
||||||
|
.Where(it => !string.IsNullOrEmpty(it.OldDbColumnName))
|
||||||
|
.Where(entityColumn => dbColumns.Any(dbColumn => entityColumn.OldDbColumnName.Equals(dbColumn.DbColumnName, StringComparison.CurrentCultureIgnoreCase)))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
|
||||||
|
var isChange = false;
|
||||||
|
foreach (var item in addColumns)
|
||||||
{
|
{
|
||||||
DbColumnInfo dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item);
|
this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
|
||||||
columns.Add(dbColumnInfo);
|
isChange = true;
|
||||||
|
}
|
||||||
|
foreach (var item in dropColumns)
|
||||||
|
{
|
||||||
|
//this.Context.DbMaintenance.DropColumn(tableName, item.DbColumnName);
|
||||||
|
//isChange = true;
|
||||||
|
}
|
||||||
|
//foreach (var item in alterColumns)
|
||||||
|
//{
|
||||||
|
// //this.Context.DbMaintenance.AddColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
|
||||||
|
// //isChange = true;
|
||||||
|
//}
|
||||||
|
foreach (var item in renameColumns)
|
||||||
|
{
|
||||||
|
throw new NotSupportedException("rename Column");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var item in entityColumns)
|
||||||
|
{
|
||||||
|
var dbColumn = dbColumns.FirstOrDefault(dc => dc.DbColumnName.Equals(item.DbColumnName, StringComparison.CurrentCultureIgnoreCase));
|
||||||
|
if (dbColumn == null) continue;
|
||||||
|
bool pkDiff, idEntityDiff;
|
||||||
|
KeyAction(item, dbColumn, out pkDiff, out idEntityDiff);
|
||||||
|
if (dbColumn != null && pkDiff && !idEntityDiff)
|
||||||
|
{
|
||||||
|
var isAdd = item.IsPrimarykey;
|
||||||
|
if (isAdd)
|
||||||
|
{
|
||||||
|
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.Context.DbMaintenance.DropConstraint(tableName, string.Format("PK_{0}_{1}", tableName, item.DbColumnName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (pkDiff || idEntityDiff)
|
||||||
|
{
|
||||||
|
ChangeKey(entityInfo, tableName, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isChange && base.IsBackupTable)
|
||||||
|
{
|
||||||
|
this.Context.DbMaintenance.BackupTable(tableName, tableName + DateTime.Now.ToString("yyyyMMddHHmmss"), MaxBackupDataRows);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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)
|
public override void NoExistLogic(EntityInfo entityInfo)
|
||||||
{
|
{
|
||||||
|
@ -46,7 +46,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
throw new NotSupportedException();
|
return "ALTER TABLE {0} ADD COLUMN {1} {2}{3}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
protected override string AlterColumnToTableSql
|
protected override string AlterColumnToTableSql
|
||||||
@ -212,6 +212,7 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
|
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
|
||||||
{
|
{
|
||||||
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||||
|
Binary file not shown.
@ -22,13 +22,16 @@ namespace OrmTest.Demo
|
|||||||
[SugarColumn(IsNullable = true,OldColumnName = "Dob")]
|
[SugarColumn(IsNullable = true,OldColumnName = "Dob")]
|
||||||
public double? Dob2 { get; set; }
|
public double? Dob2 { get; set; }
|
||||||
[SugarColumn(Length =10)]
|
[SugarColumn(Length =10)]
|
||||||
public string A { get; set; }
|
public string A1 { get; set; }
|
||||||
}
|
}
|
||||||
public class CodeTable2 {
|
public class CodeTable2 {
|
||||||
|
[SugarColumn(IsPrimaryKey =true,IsIdentity =true)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
[SugarColumn(IsIgnore =true)]
|
[SugarColumn(IsIgnore =true)]
|
||||||
public string TestId { get; set; }
|
public string TestId { get; set; }
|
||||||
|
public string Test { get; set; }
|
||||||
|
public int Test22222x2 { get; set; }
|
||||||
}
|
}
|
||||||
public class CodeFirst : DemoBase
|
public class CodeFirst : DemoBase
|
||||||
{
|
{
|
||||||
@ -46,7 +49,7 @@ namespace OrmTest.Demo
|
|||||||
//db.CodeFirst.BackupTable().InitTables(typeof(CodeTable),typeof(CodeTable2));
|
//db.CodeFirst.BackupTable().InitTables(typeof(CodeTable),typeof(CodeTable2));
|
||||||
|
|
||||||
//No backup table
|
//No backup table
|
||||||
db.CodeFirst.InitTables(typeof(CodeTable),typeof(CodeTable2));
|
db.CodeFirst.BackupTable().InitTables(typeof(CodeTable),typeof(CodeTable2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user