Update .net core project

This commit is contained in:
sunkaixuan
2022-06-30 13:53:21 +08:00
parent abb0a6fa6d
commit 2e40742bdf
4 changed files with 51 additions and 6 deletions

View File

@@ -11,7 +11,7 @@ namespace SqlSugar
{ {
public List<Root> Roots { get; set; } public List<Root> Roots { get; set; }
public object ParentList { get; set; } public List<object> ParentList { get; set; }
public EntityInfo ParentEntity { get; set; } public EntityInfo ParentEntity { get; set; }
public SqlSugarProvider Context { get; set; } public SqlSugarProvider Context { get; set; }
@@ -19,7 +19,7 @@ namespace SqlSugar
{ {
if (ParentList == null) if (ParentList == null)
{ {
ParentList = GetParentList(Roots); ParentList = GetParentList(Roots).Cast<object>().ToList();
} }
var name=ExpressionTool.GetMemberName(expression); var name=ExpressionTool.GetMemberName(expression);
var nav = this.ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name); var nav = this.ParentEntity.Columns.FirstOrDefault(x => x.PropertyName == name);
@@ -29,7 +29,7 @@ namespace SqlSugar
} }
if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne) if (nav.Navigat.NavigatType == NavigateType.OneToOne || nav.Navigat.NavigatType == NavigateType.ManyToOne)
{ {
InsertOneToOne(); InsertOneToOne<TChild>(ParentList,this.ParentEntity, name,nav);
} }
else if (nav.Navigat.NavigatType == NavigateType.OneToMany) else if (nav.Navigat.NavigatType == NavigateType.OneToMany)
{ {
@@ -52,10 +52,41 @@ namespace SqlSugar
} }
private void InsertOneToOne() private void InsertOneToOne<TChild>(List<object> parentList, EntityInfo parentEntity, string name, EntityColumnInfo nav)
{
var parentColumn = parentEntity.Columns.FirstOrDefault(it => it.PropertyName==nav.Navigat.Name);
this.ParentEntity = this.Context.EntityMaintenance.GetEntityInfo<TChild>();
var pkColumn = ParentEntity.Columns.FirstOrDefault(it=>it.IsPrimarykey==true);
if (nav.Navigat.Name2.HasValue())
{
pkColumn = ParentEntity.Columns.FirstOrDefault(it => it.PropertyName==nav.Navigat.Name2);
}
Check.Exception(pkColumn == null, $" Navigate {parentEntity.EntityName} : {name} is error ", $"导航实体 {parentEntity.EntityName} 属性 {name} 配置错误");
List<object> childList = new List<object>();
foreach (var parent in parentList)
{
var childItems=(TChild)nav.PropertyInfo.GetValue(parent);
if (childItems != null)
{
var pkValue = pkColumn.PropertyInfo.GetValue(childItems);
var pvValue = parentColumn.PropertyInfo.GetValue(parent);
if (IsDefaultValue(pvValue))
{
pvValue = pkValue;
}
if (IsDefaultValue(pvValue))
{ {
} }
}
}
parentList = childList;
}
private static bool IsDefaultValue(object pvValue)
{
return pvValue == null || pvValue == UtilMethods.GetDefaultValue(pvValue.GetType());
}
private List<Type> GetParentList<Type>(List<Type> datas) where Type : class ,new() private List<Type> GetParentList<Type>(List<Type> datas) where Type : class ,new()
{ {

View File

@@ -335,6 +335,15 @@ namespace SqlSugar
UpdateBuilder.Parameters.Add(new SugarParameter(parameterName, fieldValue)); UpdateBuilder.Parameters.Add(new SugarParameter(parameterName, fieldValue));
UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(SqlBuilder.GetTranslationColumnName(fieldName), $"{SqlBuilder.GetTranslationColumnName(fieldName)}={parameterName}")); UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>(SqlBuilder.GetTranslationColumnName(fieldName), $"{SqlBuilder.GetTranslationColumnName(fieldName)}={parameterName}"));
this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => (UpdateParameterIsNull == false && IsPrimaryKey(it)) || UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList(); this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => (UpdateParameterIsNull == false && IsPrimaryKey(it)) || UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList();
if (!this.UpdateBuilder.DbColumnInfoList.Any(it => it.DbColumnName.EqualCase(fieldName)))
{
this.UpdateBuilder.DbColumnInfoList.Add(new DbColumnInfo()
{
DbColumnName=fieldName,
Value=fieldValue,
PropertyName=fieldName
});
}
AppendSets(); AppendSets();
return this; return this;
} }

View File

@@ -121,6 +121,7 @@ namespace SqlSugar
if (sqlParameter.DbType == System.Data.DbType.Guid) if (sqlParameter.DbType == System.Data.DbType.Guid)
{ {
sqlParameter.DbType = System.Data.DbType.String; sqlParameter.DbType = System.Data.DbType.String;
if(sqlParameter.Value != DBNull.Value)
sqlParameter.Value = sqlParameter.Value.ToString(); sqlParameter.Value = sqlParameter.Value.ToString();
} }
if (parameter.Direction == 0) if (parameter.Direction == 0)

View File

@@ -216,6 +216,10 @@ namespace SqlSugar
Type type = Nullable.GetUnderlyingType(oldType); Type type = Nullable.GetUnderlyingType(oldType);
return type == null ? oldType : type; return type == null ? oldType : type;
} }
public static object GetDefaultValue(Type type)
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
public static string ReplaceSqlParameter(string itemSql, SugarParameter itemParameter, string newName) public static string ReplaceSqlParameter(string itemSql, SugarParameter itemParameter, string newName)
{ {
itemSql = Regex.Replace(itemSql, string.Format(@"{0} ", "\\" + itemParameter.ParameterName), newName + " ", RegexOptions.IgnoreCase); itemSql = Regex.Replace(itemSql, string.Format(@"{0} ", "\\" + itemParameter.ParameterName), newName + " ", RegexOptions.IgnoreCase);