You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
3.7 KiB
86 lines
3.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace SqlSugar
|
|
{
|
|
public class SqliteCodeFirst : CodeFirstProvider
|
|
{
|
|
public override void ExistLogic(EntityInfo entityInfo)
|
|
{
|
|
var tableName = GetTableName(entityInfo);
|
|
var backupName = tableName + DateTime.Now.ToString("yyyyMMddHHmmss");
|
|
Check.Exception(entityInfo.Columns.Count(it => it.IsPrimarykey)> 1, "Use Code First ,The primary key must not exceed 1");
|
|
var columns = new List<DbColumnInfo>();
|
|
if (entityInfo.Columns.HasValue())
|
|
{
|
|
foreach (var item in entityInfo.Columns.Where(it => !it.IsIgnore))
|
|
{
|
|
var dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item);
|
|
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);
|
|
var backupName=tableName+DateTime.Now.ToString("yyyyMMddHHmmss");
|
|
Check.Exception(entityInfo.Columns.Count(it => it.IsPrimarykey)> 1, "Use Code First ,The primary key must not exceed 1");
|
|
var columns = new List<DbColumnInfo>();
|
|
if (entityInfo.Columns.HasValue())
|
|
{
|
|
foreach (var item in entityInfo.Columns.Where(it=> !it.IsIgnore))
|
|
{
|
|
var 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)
|
|
{
|
|
var propertyType = UtilMethods.GetUnderType(item.PropertyInfo);
|
|
var result = new DbColumnInfo
|
|
{
|
|
TableId = entityInfo.Columns.IndexOf(item),
|
|
DbColumnName = item.DbColumnName.HasValue() ? item.DbColumnName : item.PropertyName,
|
|
IsPrimarykey = item.IsPrimarykey,
|
|
IsIdentity = item.IsIdentity,
|
|
TableName = tableName,
|
|
IsNullable = item.IsNullable,
|
|
DefaultValue = item.DefaultValue,
|
|
ColumnDescription = item.ColumnDescription,
|
|
Length = item.Length
|
|
};
|
|
GetDbType(item, propertyType, result);
|
|
if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)
|
|
{
|
|
result.Length = 1;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
protected override void ConvertColumns(List<DbColumnInfo> dbColumns)
|
|
{
|
|
foreach (var item in dbColumns)
|
|
{
|
|
if (item.DataType == "DateTime")
|
|
{
|
|
item.Length = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
|
|
{
|
|
this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
|
|
if (!item.IsPrimarykey)
|
|
this.Context.DbMaintenance.DropConstraint(tableName,null);
|
|
if (item.IsPrimarykey)
|
|
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
|
}
|
|
}
|
|
}
|