Skip to content

Commit 965e03f

Browse files
Update source to latest version
Update source to latest version
1 parent 52431e0 commit 965e03f

File tree

3 files changed

+83
-43
lines changed

3 files changed

+83
-43
lines changed

src/shared/Z.EF.Plus.QueryIncludeFilterCore.Shared/QueryIncludeFilterNullCollection.cs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,7 @@ public static void CheckNullRecursive(object currentItem, List<string> paths, in
7474

7575
if (value == null)
7676
{
77-
if (property.PropertyType.GetGenericArguments().Length == 1)
78-
{
79-
var genericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();
80-
81-
if (genericTypeDefinition == typeof(ICollection<>))
82-
{
83-
value = Activator.CreateInstance(typeof(HashSet<>).MakeGenericType(property.PropertyType.GetGenericArguments()[0]));
84-
}
85-
else if(genericTypeDefinition == typeof(IList<>))
86-
{
87-
value = Activator.CreateInstance(typeof(List<>).MakeGenericType(property.PropertyType.GetGenericArguments()[0]));
88-
}
89-
else
90-
{
91-
value = Activator.CreateInstance(property.PropertyType);
92-
}
93-
94-
accessor.SetValue(currentItem, value);
95-
}
96-
77+
CheckAndSetCollection(accessor, currentItem, property.PropertyType, property.PropertyType);
9778
return;
9879
}
9980

@@ -104,5 +85,38 @@ public static void CheckNullRecursive(object currentItem, List<string> paths, in
10485
}
10586
}
10687
}
88+
89+
private static void CheckAndSetCollection(PropertyOrFieldAccessor accessor, object currentItem, Type propertyType, Type originalType)
90+
{
91+
if (propertyType.GetGenericArguments().Length == 1)
92+
{
93+
object value;
94+
95+
var genericTypeDefinition = propertyType.GetGenericTypeDefinition();
96+
97+
if (genericTypeDefinition == typeof(ICollection<>))
98+
{
99+
value = Activator.CreateInstance(typeof(HashSet<>).MakeGenericType(propertyType.GetGenericArguments()[0]));
100+
}
101+
else if (genericTypeDefinition == typeof(IList<>))
102+
{
103+
value = Activator.CreateInstance(typeof(List<>).MakeGenericType(propertyType.GetGenericArguments()[0]));
104+
}
105+
else
106+
{
107+
// Create an instance of the original type since we know it implement one of the type supported: List<A>
108+
value = Activator.CreateInstance(originalType);
109+
}
110+
111+
accessor.SetValue(currentItem, value);
112+
}
113+
else
114+
{
115+
if (propertyType.BaseType != null && propertyType.BaseType != typeof(object))
116+
{
117+
CheckAndSetCollection(accessor, currentItem, propertyType.BaseType, originalType);
118+
}
119+
}
120+
}
107121
}
108122
}

src/shared/Z.EF.Plus.QueryIncludeOptimized.Shared/QueryIncludeOptimizedNullCollection.cs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,13 @@ public static void CheckNullRecursive(object currentItem, List<string> paths, in
6868

6969
if (property != null)
7070
{
71-
var accessor = new PropertyOrFieldAccessor(property);
71+
var accessor = new PropertyOrFieldAccessor(property);
7272

73-
var value = accessor.GetValue(currentItem);
73+
var value = accessor.GetValue(currentItem);
7474

7575
if (value == null)
7676
{
77-
if (property.PropertyType.GetGenericArguments().Length == 1)
78-
{
79-
var genericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();
80-
81-
if (genericTypeDefinition == typeof(ICollection<>))
82-
{
83-
value = Activator.CreateInstance(typeof(HashSet<>).MakeGenericType(property.PropertyType.GetGenericArguments()[0]));
84-
}
85-
else if(genericTypeDefinition == typeof(IList<>))
86-
{
87-
value = Activator.CreateInstance(typeof(List<>).MakeGenericType(property.PropertyType.GetGenericArguments()[0]));
88-
}
89-
else
90-
{
91-
value = Activator.CreateInstance(property.PropertyType);
92-
}
93-
94-
accessor.SetValue(currentItem, value);
95-
}
96-
77+
CheckAndSetCollection(accessor, currentItem, property.PropertyType, property.PropertyType);
9778
return;
9879
}
9980

@@ -104,5 +85,38 @@ public static void CheckNullRecursive(object currentItem, List<string> paths, in
10485
}
10586
}
10687
}
88+
89+
private static void CheckAndSetCollection(PropertyOrFieldAccessor accessor, object currentItem, Type propertyType, Type originalType)
90+
{
91+
if (propertyType.GetGenericArguments().Length == 1)
92+
{
93+
object value;
94+
95+
var genericTypeDefinition = propertyType.GetGenericTypeDefinition();
96+
97+
if (genericTypeDefinition == typeof(ICollection<>))
98+
{
99+
value = Activator.CreateInstance(typeof(HashSet<>).MakeGenericType(propertyType.GetGenericArguments()[0]));
100+
}
101+
else if (genericTypeDefinition == typeof(IList<>))
102+
{
103+
value = Activator.CreateInstance(typeof(List<>).MakeGenericType(propertyType.GetGenericArguments()[0]));
104+
}
105+
else
106+
{
107+
// Create an instance of the original type since we know it implement one of the type supported: List<A>
108+
value = Activator.CreateInstance(originalType);
109+
}
110+
111+
accessor.SetValue(currentItem, value);
112+
}
113+
else
114+
{
115+
if (propertyType.BaseType != null && propertyType.BaseType != typeof(object))
116+
{
117+
CheckAndSetCollection(accessor, currentItem, propertyType.BaseType, originalType);
118+
}
119+
}
120+
}
107121
}
108122
}

src/shared/Z.EF.Plus._Core.Shared/Model/Model/Model.GetDatabaseFirst.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
// More projects: http://www.zzzprojects.com/
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

8+
89
#if FULL || BATCH_DELETE || BATCH_UPDATE
910
#if EF5 || EF6
1011
using System;
1112
using System.Data.Entity;
1213
using System.Linq;
1314
using System.Xml.Linq;
15+
using System.Collections.Generic;
1416
using Z.EntityFramework.Plus.Internal.Core.Infrastructure;
1517
using Z.EntityFramework.Plus.Internal.Core.Mapping;
1618
using Z.EntityFramework.Plus.Internal.Core.SchemaObjectModel;
@@ -27,7 +29,17 @@ internal static partial class Model
2729
internal static DbModelPlus GetDatabaseFirst(DbContext context)
2830
{
2931
var modelSplit = "---zzz_multi_model_split_zzz---";
30-
var modelNames = context.GetModelNames();
32+
33+
List<string> modelNames = new List<string>();
34+
35+
36+
try
37+
{
38+
modelNames = context.GetModelNames();
39+
}
40+
catch
41+
{
42+
}
3143

3244
if (modelNames.Count == 0)
3345
{

0 commit comments

Comments
 (0)