!15 增加 sqlite code first 对多主键建表的支持

Merge pull request !15 from tomato2313/master
This commit is contained in:
sunkaixuan 2022-04-15 11:46:13 +00:00 committed by Gitee
commit e90d428fa6
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 17 additions and 6 deletions

View File

@ -91,7 +91,7 @@ namespace SqlSugar
{
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");
//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())
{

View File

@ -84,7 +84,7 @@ namespace SqlSugar
{
get
{
return "CREATE TABLE {0}(\r\n{1} )";
return "CREATE TABLE {0}(\r\n{1} $PrimaryKey )";
}
}
protected override string CreateTableColumn
@ -234,9 +234,9 @@ namespace SqlSugar
}
}
protected override string CreateIndexSql
protected override string CreateIndexSql
{
get
get
{
return "CREATE {3} INDEX Index_{0}_{2} ON {0}({1})";
}
@ -362,7 +362,7 @@ namespace SqlSugar
{
return true;
}
public override bool BackupTable(string oldTableName, string newTableName, int maxBackupDataRows = int.MaxValue)
{
@ -390,10 +390,21 @@ namespace SqlSugar
}
}
string sql = GetCreateTableSql(tableName, columns);
if (!isCreatePrimaryKey)
string primaryKeyInfo = null;
if (!isCreatePrimaryKey || columns.Count(it => it.IsPrimarykey) > 1)
{
sql = sql.Replace("PRIMARY KEY AUTOINCREMENT", "").Replace("PRIMARY KEY", "");
}
if (columns.Count(it => it.IsPrimarykey) > 1 && isCreatePrimaryKey)
{
primaryKeyInfo = string.Format(",\r\n Primary key({0})", string.Join(",", columns.Where(it => it.IsPrimarykey).Select(it => this.SqlBuilder.GetTranslationColumnName(it.DbColumnName))));
primaryKeyInfo = primaryKeyInfo.Replace("`", "\"");
}
sql = sql.Replace("$PrimaryKey", primaryKeyInfo);
this.Context.Ado.ExecuteCommand(sql);
return true;
}