mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Support CodeFirst Multiple Primary Key
This commit is contained in:
parent
f87ece6d84
commit
c8f87dc029
@ -31,6 +31,15 @@ namespace OrmTest.Demo
|
||||
[SugarColumn(IsIgnore =true)]
|
||||
public string TestId { get; set; }
|
||||
}
|
||||
public class CodeTable3
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public int Id { get; set; }
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public string Name { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public string TestId { get; set; }
|
||||
}
|
||||
public class CodeFirst : DemoBase
|
||||
{
|
||||
public static void Init()
|
||||
@ -47,7 +56,7 @@ namespace OrmTest.Demo
|
||||
//db.CodeFirst.BackupTable().InitTables(typeof(CodeTable),typeof(CodeTable2));
|
||||
|
||||
//No backup table
|
||||
db.CodeFirst.InitTables(typeof(CodeTable),typeof(CodeTable2));
|
||||
db.CodeFirst.InitTables(typeof(CodeTable),typeof(CodeTable2),typeof(CodeTable3));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,15 @@ namespace OrmTest.Demo
|
||||
[SugarColumn(IsIgnore =true)]
|
||||
public string TestId { get; set; }
|
||||
}
|
||||
public class CodeTable3
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey =true)]
|
||||
public int Id { get; set; }
|
||||
[SugarColumn(IsPrimaryKey = true)]
|
||||
public string Name { get; set; }
|
||||
[SugarColumn(IsIgnore = true)]
|
||||
public string TestId { get; set; }
|
||||
}
|
||||
public class CodeFirst : DemoBase
|
||||
{
|
||||
public static void Init()
|
||||
@ -51,6 +60,9 @@ namespace OrmTest.Demo
|
||||
|
||||
//No backup table
|
||||
db.CodeFirst.SetStringDefaultLength(10).InitTables(typeof(CodeTable),typeof(CodeTable2));
|
||||
|
||||
|
||||
db.CodeFirst.InitTables(typeof(CodeTable3));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ namespace SqlSugar
|
||||
public virtual void NoExistLogic(EntityInfo entityInfo)
|
||||
{
|
||||
var tableName = GetTableName(entityInfo);
|
||||
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())
|
||||
{
|
||||
@ -110,7 +110,7 @@ namespace SqlSugar
|
||||
{
|
||||
if (entityInfo.Columns.HasValue())
|
||||
{
|
||||
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, "Multiple primary keys do not support modifications");
|
||||
|
||||
var tableName = GetTableName(entityInfo);
|
||||
var dbColumns = this.Context.DbMaintenance.GetColumnInfosByTableName(tableName);
|
||||
|
@ -140,6 +140,15 @@ namespace SqlSugar
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool AddPrimaryKeys(string tableName, string[] columnNames)
|
||||
{
|
||||
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
||||
var columnName = string.Join(",", columnNames);
|
||||
string sql = string.Format(this.AddPrimaryKeySql, tableName, string.Format("PK_{0}_{1}", this.SqlBuilder.GetNoTranslationColumnName(columnNames.First()), this.SqlBuilder.GetNoTranslationColumnName(columnNames.First())), columnName);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
public virtual bool AddColumn(string tableName, DbColumnInfo columnInfo)
|
||||
{
|
||||
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
||||
|
@ -32,6 +32,7 @@ namespace SqlSugar
|
||||
bool AddColumn(string tableName, DbColumnInfo column);
|
||||
bool UpdateColumn(string tableName, DbColumnInfo column);
|
||||
bool AddPrimaryKey(string tableName,string columnName);
|
||||
bool AddPrimaryKeys(string tableName, string [] columnNames);
|
||||
bool DropConstraint(string tableName, string constraintName);
|
||||
bool BackupDataBase(string databaseName,string fullFileName);
|
||||
bool BackupTable(string oldTableName, string newTableName, int maxBackupDataRows = int.MaxValue);
|
||||
|
@ -10,7 +10,7 @@ namespace SqlSugar
|
||||
public override void NoExistLogic(EntityInfo entityInfo)
|
||||
{
|
||||
var tableName = GetTableName(entityInfo);
|
||||
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())
|
||||
{
|
||||
|
@ -282,9 +282,16 @@ namespace SqlSugar
|
||||
if (isCreatePrimaryKey)
|
||||
{
|
||||
var pkColumns = columns.Where(it => it.IsPrimarykey).ToList();
|
||||
foreach (var item in pkColumns)
|
||||
if (pkColumns.Count > 1)
|
||||
{
|
||||
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||
this.Context.DbMaintenance.AddPrimaryKeys(tableName, pkColumns.Select(it=>it.DbColumnName).ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in pkColumns)
|
||||
{
|
||||
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user