mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
init ArrayToMany
This commit is contained in:
parent
24f55fa0f4
commit
8248057d5c
@ -155,8 +155,9 @@ namespace FreeSql
|
|||||||
if (tref == null) return;
|
if (tref == null) return;
|
||||||
switch (tref.RefType)
|
switch (tref.RefType)
|
||||||
{
|
{
|
||||||
case Internal.Model.TableRefType.OneToOne:
|
case TableRefType.OneToOne:
|
||||||
case Internal.Model.TableRefType.ManyToOne:
|
case TableRefType.ManyToOne:
|
||||||
|
case TableRefType.ArrayToMany:
|
||||||
throw new ArgumentException(DbContextStrings.PropertyOfType_IsNot_OneToManyOrManyToMany(_table.Type.FullName, propertyName));
|
throw new ArgumentException(DbContextStrings.PropertyOfType_IsNot_OneToManyOrManyToMany(_table.Type.FullName, propertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,7 +167,7 @@ namespace FreeSql
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
await AddOrUpdateNavigateAsync(item, false, propertyName, cancellationToken);
|
await AddOrUpdateNavigateAsync(item, false, propertyName, cancellationToken);
|
||||||
if (tref.RefType == Internal.Model.TableRefType.OneToMany)
|
if (tref.RefType == TableRefType.OneToMany)
|
||||||
{
|
{
|
||||||
await DbContextFlushCommandAsync(cancellationToken);
|
await DbContextFlushCommandAsync(cancellationToken);
|
||||||
//删除没有保存的数据,求出主体的条件
|
//删除没有保存的数据,求出主体的条件
|
||||||
@ -213,7 +214,7 @@ namespace FreeSql
|
|||||||
DbSet<object> refSet = null;
|
DbSet<object> refSet = null;
|
||||||
switch (tref.RefType)
|
switch (tref.RefType)
|
||||||
{
|
{
|
||||||
case Internal.Model.TableRefType.OneToOne:
|
case TableRefType.OneToOne:
|
||||||
refSet = GetDbSetObject(tref.RefEntityType);
|
refSet = GetDbSetObject(tref.RefEntityType);
|
||||||
var propValItem = GetItemValue(item, prop);
|
var propValItem = GetItemValue(item, prop);
|
||||||
if (propValItem == null) return;
|
if (propValItem == null) return;
|
||||||
@ -225,7 +226,8 @@ namespace FreeSql
|
|||||||
if (isAdd) await refSet.AddAsync(propValItem);
|
if (isAdd) await refSet.AddAsync(propValItem);
|
||||||
else await refSet.AddOrUpdateAsync(propValItem);
|
else await refSet.AddOrUpdateAsync(propValItem);
|
||||||
return;
|
return;
|
||||||
case Internal.Model.TableRefType.ManyToOne:
|
case TableRefType.ManyToOne:
|
||||||
|
case TableRefType.ArrayToMany:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,7 +236,7 @@ namespace FreeSql
|
|||||||
refSet = GetDbSetObject(tref.RefEntityType);
|
refSet = GetDbSetObject(tref.RefEntityType);
|
||||||
switch (tref.RefType)
|
switch (tref.RefType)
|
||||||
{
|
{
|
||||||
case Internal.Model.TableRefType.ManyToMany:
|
case TableRefType.ManyToMany:
|
||||||
var curList = new List<object>();
|
var curList = new List<object>();
|
||||||
foreach (var propValItem in propValEach)
|
foreach (var propValItem in propValEach)
|
||||||
{
|
{
|
||||||
@ -327,7 +329,7 @@ namespace FreeSql
|
|||||||
await midSet.AddRangeAsync(midListAdd, cancellationToken);
|
await midSet.AddRangeAsync(midListAdd, cancellationToken);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Internal.Model.TableRefType.OneToMany:
|
case TableRefType.OneToMany:
|
||||||
var addList = new List<object>();
|
var addList = new List<object>();
|
||||||
var addOrUpdateList = new List<object>();
|
var addOrUpdateList = new List<object>();
|
||||||
foreach (var propValItem in propValEach)
|
foreach (var propValItem in propValEach)
|
||||||
|
@ -166,8 +166,9 @@ namespace FreeSql
|
|||||||
if (tref == null) return;
|
if (tref == null) return;
|
||||||
switch (tref.RefType)
|
switch (tref.RefType)
|
||||||
{
|
{
|
||||||
case Internal.Model.TableRefType.OneToOne:
|
case TableRefType.OneToOne:
|
||||||
case Internal.Model.TableRefType.ManyToOne:
|
case TableRefType.ManyToOne:
|
||||||
|
case TableRefType.ArrayToMany:
|
||||||
throw new ArgumentException(DbContextStrings.PropertyOfType_IsNot_OneToManyOrManyToMany(_table.Type.FullName, propertyName));
|
throw new ArgumentException(DbContextStrings.PropertyOfType_IsNot_OneToManyOrManyToMany(_table.Type.FullName, propertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +178,7 @@ namespace FreeSql
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
AddOrUpdateNavigate(item, false, propertyName);
|
AddOrUpdateNavigate(item, false, propertyName);
|
||||||
if (tref.RefType == Internal.Model.TableRefType.OneToMany)
|
if (tref.RefType == TableRefType.OneToMany)
|
||||||
{
|
{
|
||||||
DbContextFlushCommand();
|
DbContextFlushCommand();
|
||||||
//删除没有保存的数据,求出主体的条件
|
//删除没有保存的数据,求出主体的条件
|
||||||
@ -224,7 +225,7 @@ namespace FreeSql
|
|||||||
DbSet<object> refSet = null;
|
DbSet<object> refSet = null;
|
||||||
switch (tref.RefType)
|
switch (tref.RefType)
|
||||||
{
|
{
|
||||||
case Internal.Model.TableRefType.OneToOne:
|
case TableRefType.OneToOne:
|
||||||
refSet = GetDbSetObject(tref.RefEntityType);
|
refSet = GetDbSetObject(tref.RefEntityType);
|
||||||
var propValItem = GetItemValue(item, prop);
|
var propValItem = GetItemValue(item, prop);
|
||||||
if (propValItem == null) return;
|
if (propValItem == null) return;
|
||||||
@ -236,7 +237,8 @@ namespace FreeSql
|
|||||||
if (isAdd) refSet.Add(propValItem);
|
if (isAdd) refSet.Add(propValItem);
|
||||||
else refSet.AddOrUpdate(propValItem);
|
else refSet.AddOrUpdate(propValItem);
|
||||||
return;
|
return;
|
||||||
case Internal.Model.TableRefType.ManyToOne:
|
case TableRefType.ManyToOne:
|
||||||
|
case TableRefType.ArrayToMany:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -714,7 +716,7 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
return tb.Properties.Where(a => tb.ColumnsByCs.ContainsKey(a.Key) == false)
|
return tb.Properties.Where(a => tb.ColumnsByCs.ContainsKey(a.Key) == false)
|
||||||
.Select(a => new NativeTuple<TableRef, PropertyInfo>(tb.GetTableRef(a.Key, false), a.Value))
|
.Select(a => new NativeTuple<TableRef, PropertyInfo>(tb.GetTableRef(a.Key, false), a.Value))
|
||||||
.Where(a => a.Item1 != null && a.Item1.RefType != TableRefType.ManyToOne)
|
.Where(a => a.Item1 != null && new[] { TableRefType.OneToOne, TableRefType.OneToMany, TableRefType.ManyToMany }.Contains(a.Item1.RefType))
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
void LocalEach(DbSet<object> dbset, IEnumerable<object> items, bool isOneToOne)
|
void LocalEach(DbSet<object> dbset, IEnumerable<object> items, bool isOneToOne)
|
||||||
|
@ -154,6 +154,7 @@ namespace FreeSql.Internal
|
|||||||
{
|
{
|
||||||
case TableRefType.ManyToMany:
|
case TableRefType.ManyToMany:
|
||||||
case TableRefType.OneToMany:
|
case TableRefType.OneToMany:
|
||||||
|
case TableRefType.ArrayToMany:
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (_tables.Any(a => a.Alias == $"{map.First().Table.Alias}__{memProp.Name}") == false) continue;
|
if (_tables.Any(a => a.Alias == $"{map.First().Table.Alias}__{memProp.Name}") == false) continue;
|
||||||
@ -2194,6 +2195,8 @@ namespace FreeSql.Internal
|
|||||||
for (var tidx = 0; tidx < memberTbref.Columns.Count; tidx++)
|
for (var tidx = 0; tidx < memberTbref.Columns.Count; tidx++)
|
||||||
select.Where($"{select._tables[0].Alias}.{commonExp._common.QuoteSqlName(memberTbref.RefColumns[tidx].Attribute.Name)} = {omtReftbname}.{commonExp._common.QuoteSqlName(memberTbref.Columns[tidx].Attribute.Name)}");
|
select.Where($"{select._tables[0].Alias}.{commonExp._common.QuoteSqlName(memberTbref.RefColumns[tidx].Attribute.Name)} = {omtReftbname}.{commonExp._common.QuoteSqlName(memberTbref.Columns[tidx].Attribute.Name)}");
|
||||||
break;
|
break;
|
||||||
|
case TableRefType.ArrayToMany:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2241,6 +2244,8 @@ namespace FreeSql.Internal
|
|||||||
if (select != null) return;
|
if (select != null) return;
|
||||||
LocalInitSelectProvider();
|
LocalInitSelectProvider();
|
||||||
continue;
|
continue;
|
||||||
|
case TableRefType.ArrayToMany:
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (exp4.NodeType == ExpressionType.Call)
|
if (exp4.NodeType == ExpressionType.Call)
|
||||||
|
@ -430,6 +430,8 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
var curTb = _commonUtils.GetTableByEntity(exp.Type);
|
var curTb = _commonUtils.GetTableByEntity(exp.Type);
|
||||||
_commonExpression.ExpressionWhereLambda(_tables, Expression.MakeMemberAccess(exp, curTb.Properties[curTb.ColumnsByCs.First().Value.CsName]), null, null, null);
|
_commonExpression.ExpressionWhereLambda(_tables, Expression.MakeMemberAccess(exp, curTb.Properties[curTb.ColumnsByCs.First().Value.CsName]), null, null, null);
|
||||||
break;
|
break;
|
||||||
|
case TableRefType.ArrayToMany:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -1172,6 +1174,8 @@ namespace FreeSql.Internal.CommonProvider
|
|||||||
dicList.Clear();
|
dicList.Clear();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case TableRefType.ArrayToMany:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -150,6 +150,6 @@ namespace FreeSql.Internal.Model
|
|||||||
}
|
}
|
||||||
public enum TableRefType
|
public enum TableRefType
|
||||||
{
|
{
|
||||||
OneToOne, ManyToOne, OneToMany, ManyToMany
|
OneToOne, ManyToOne, OneToMany, ManyToMany, ArrayToMany
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -593,7 +593,7 @@ namespace FreeSql.Internal
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
throw new Exception(CoreStrings.LazyLoading_CompilationError(trytbTypeName, ex.Message, cscode)); ;
|
throw new Exception(CoreStrings.LazyLoading_CompilationError(trytbTypeName, ex.Message, cscode));
|
||||||
}
|
}
|
||||||
var type = assembly.GetExportedTypes()/*.DefinedTypes*/.Where(a => a.FullName.EndsWith(trytbTypeLazyName)).FirstOrDefault();
|
var type = assembly.GetExportedTypes()/*.DefinedTypes*/.Where(a => a.FullName.EndsWith(trytbTypeLazyName)).FirstOrDefault();
|
||||||
trytb.TypeLazy = type;
|
trytb.TypeLazy = type;
|
||||||
@ -989,8 +989,113 @@ namespace FreeSql.Internal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ //One To Many
|
{
|
||||||
|
var isArrayToMany = false;
|
||||||
|
var lmbdWhere = isLazy ? new StringBuilder() : null;
|
||||||
|
//Pgsql Array[] To Many
|
||||||
|
if (common._orm.Ado.DataType == DataType.PostgreSQL)
|
||||||
|
{
|
||||||
|
//class User {
|
||||||
|
// public int[] RoleIds { get; set; }
|
||||||
|
// [Navigate(nameof(RoleIds))]
|
||||||
|
// public Role[] Roles { get; set; }
|
||||||
|
//}
|
||||||
|
//class Role {
|
||||||
|
// [Navigate(nameof(User.RoleIds))]
|
||||||
|
// public User[] Users { get; set; }
|
||||||
|
//}
|
||||||
|
ColumnInfo trycol = null;
|
||||||
|
if (tbref.Primarys.Length == 1)
|
||||||
|
{
|
||||||
|
if (pnvBind?.Length == 1)
|
||||||
|
{
|
||||||
|
if (trytb.ColumnsByCs.TryGetValue(pnvBind[0], out trycol))
|
||||||
|
{
|
||||||
|
if (trycol.CsType.IsArray == true && tbref.Primarys[0].CsType.NullableTypeOrThis() != trycol.CsType.GetElementType().NullableTypeOrThis())
|
||||||
|
{
|
||||||
|
nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 特性 [Navigate] 解析错误,{trytbTypeName}.{trycol.CsName} 数组元素 与 {tbrefTypeName}.{tbref.Primarys[0].CsName} 类型不符");
|
||||||
|
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
|
||||||
|
//if (isLazy) throw nvref.Exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nvref.Exception == null && trycol == null)
|
||||||
|
{
|
||||||
|
var findtbrefPkCsName = tbref.Primarys[0].CsName.TrimStart('_');
|
||||||
|
if (findtbrefPkCsName.StartsWith(trytb.Type.Name, StringComparison.CurrentCultureIgnoreCase)) findtbrefPkCsName = findtbrefPkCsName.Substring(trytb.Type.Name.Length).TrimStart('_');
|
||||||
|
var findtrytb = pnv.Name;
|
||||||
|
if (findtrytb.EndsWith($"{tbref.CsName}s", StringComparison.CurrentCultureIgnoreCase)) findtrytb = findtrytb.Substring(0, findtrytb.Length - tbref.CsName.Length - 1);
|
||||||
|
findtrytb += tbref.CsName;
|
||||||
|
if (
|
||||||
|
tbref.ColumnsByCs.TryGetValue($"{findtrytb}{findtbrefPkCsName}", out trycol) == false && //骆峰命名
|
||||||
|
tbref.ColumnsByCs.TryGetValue($"{findtrytb}_{findtbrefPkCsName}", out trycol) == false //下划线命名
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
if (trycol != null && tbref.Primarys[0].CsType.NullableTypeOrThis() != trycol.CsType.GetElementType().NullableTypeOrThis())
|
||||||
|
trycol = null;
|
||||||
|
}
|
||||||
|
isArrayToMany = trycol != null;
|
||||||
|
if (isArrayToMany)
|
||||||
|
{
|
||||||
|
lmbdWhere.Append("this.").Append(trycol.CsName).Append(".Contains(a.").Append(tbref.Primarys[0].CsName).Append(")");
|
||||||
|
nvref.Columns.Add(trycol);
|
||||||
|
nvref.RefColumns.Add(tbref.Primarys[0]);
|
||||||
|
nvref.RefEntityType = tbref.Type;
|
||||||
|
nvref.RefType = TableRefType.ArrayToMany;
|
||||||
|
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nvref.Exception == null && trytb.Primarys.Length == 1 && isArrayToMany == false)
|
||||||
|
{
|
||||||
|
if (pnvBind?.Length == 1)
|
||||||
|
{
|
||||||
|
if (tbref.ColumnsByCs.TryGetValue(pnvBind[0], out trycol))
|
||||||
|
{
|
||||||
|
if (trycol.CsType.IsArray == true && trytb.Primarys[0].CsType.NullableTypeOrThis() != trycol.CsType.GetElementType().NullableTypeOrThis())
|
||||||
|
{
|
||||||
|
nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 特性 [Navigate] 解析错误,{trytbTypeName}.{trytb.Primarys[0].CsName} 与 {tbrefTypeName}.{trycol.CsName} 数组元素类型不符");
|
||||||
|
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
|
||||||
|
//if (isLazy) throw nvref.Exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nvref.Exception == null && trycol == null)
|
||||||
|
{
|
||||||
|
var findtrytbPkCsName = trytb.Primarys[0].CsName.TrimStart('_');
|
||||||
|
if (findtrytbPkCsName.StartsWith(trytb.Type.Name, StringComparison.CurrentCultureIgnoreCase)) findtrytbPkCsName = findtrytbPkCsName.Substring(trytb.Type.Name.Length).TrimStart('_');
|
||||||
|
var findtrytb = pnv.Name;
|
||||||
|
if (findtrytb.EndsWith($"{tbref.CsName}s", StringComparison.CurrentCultureIgnoreCase)) findtrytb = findtrytb.Substring(0, findtrytb.Length - tbref.CsName.Length - 1);
|
||||||
|
findtrytb += trytb.CsName;
|
||||||
|
if (
|
||||||
|
tbref.ColumnsByCs.TryGetValue($"{findtrytb}{findtrytbPkCsName}", out trycol) == false && //骆峰命名
|
||||||
|
tbref.ColumnsByCs.TryGetValue($"{findtrytb}_{findtrytbPkCsName}", out trycol) == false //下划线命名
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
if (trycol != null && trytb.Primarys[0].CsType.NullableTypeOrThis() != trycol.CsType.GetElementType().NullableTypeOrThis())
|
||||||
|
trycol = null;
|
||||||
|
}
|
||||||
|
isArrayToMany = trycol != null;
|
||||||
|
if (isArrayToMany)
|
||||||
|
{
|
||||||
|
lmbdWhere.Append("a.").Append(trycol.CsName).Append(".Contains(this.").Append(trytb.Primarys[0].CsName).Append(")");
|
||||||
|
nvref.Columns.Add(tbref.Primarys[0]);
|
||||||
|
nvref.RefColumns.Add(trycol);
|
||||||
|
nvref.RefEntityType = tbref.Type;
|
||||||
|
nvref.RefType = TableRefType.ArrayToMany;
|
||||||
|
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
PropertyInfo refprop = null;
|
||||||
|
if (isArrayToMany == false)
|
||||||
|
{
|
||||||
List<ColumnInfo> bindColumns = new List<ColumnInfo>();
|
List<ColumnInfo> bindColumns = new List<ColumnInfo>();
|
||||||
|
//One To Many
|
||||||
if (pnvBind != null)
|
if (pnvBind != null)
|
||||||
{
|
{
|
||||||
foreach (var bi in pnvBind)
|
foreach (var bi in pnvBind)
|
||||||
@ -1006,10 +1111,8 @@ namespace FreeSql.Internal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PropertyInfo refprop = null;
|
|
||||||
var refcols = tbref.Properties.Where(z => z.Value.PropertyType == trytb.Type);
|
var refcols = tbref.Properties.Where(z => z.Value.PropertyType == trytb.Type);
|
||||||
refprop = refcols.Count() == 1 ? refcols.First().Value : null;
|
refprop = refcols.Count() == 1 ? refcols.First().Value : null;
|
||||||
var lmbdWhere = isLazy ? new StringBuilder() : null;
|
|
||||||
|
|
||||||
if (nvref.Exception == null && bindColumns.Any() && bindColumns.Count != trytb.Primarys.Length)
|
if (nvref.Exception == null && bindColumns.Any() && bindColumns.Count != trytb.Primarys.Length)
|
||||||
{
|
{
|
||||||
@ -1094,6 +1197,7 @@ namespace FreeSql.Internal
|
|||||||
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
|
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
if (isLazy)
|
if (isLazy)
|
||||||
{
|
{
|
||||||
cscode.Append(" private bool __lazy__").Append(pnv.Name).AppendLine(" = false;")
|
cscode.Append(" private bool __lazy__").Append(pnv.Name).AppendLine(" = false;")
|
||||||
@ -1133,7 +1237,8 @@ namespace FreeSql.Internal
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{ //一对一、多对一
|
{
|
||||||
|
//一对一、多对一
|
||||||
var tbref = pnv.PropertyType == trytb.Type ? trytb : GetTableByEntity(pnv.PropertyType, common); //可能是父子关系
|
var tbref = pnv.PropertyType == trytb.Type ? trytb : GetTableByEntity(pnv.PropertyType, common); //可能是父子关系
|
||||||
if (tbref == null) return;
|
if (tbref == null) return;
|
||||||
if (tbref.Primarys.Any() == false)
|
if (tbref.Primarys.Any() == false)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user