Skip to content

Commit 34c7da7

Browse files
committed
fixed NotMapped, Include
1 parent 9fa9e54 commit 34c7da7

File tree

13 files changed

+145
-22
lines changed

13 files changed

+145
-22
lines changed

src/Example/Data/Context.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class Context : DbContext
1414

1515
public DbSet<Content> Contents { get; set; }
1616

17+
public DbSet<ContentEntry> ContentEntries { get; set; }
18+
1719
public DbSet<Setting> Settings { get; set; }
1820

1921
public DbSet<Messurement> Messurements { get; set; }

src/Example/Data/Entities/Content.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ public class Content : Base
1414
public int UserId { get; set; }
1515

1616
public virtual User User { get; set; }
17+
18+
public virtual List<ContentEntry> Entries { get; set; }
1719
}
1820
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
using System.Text;
5+
6+
namespace Example.Data.Entities
7+
{
8+
public class ContentEntry : Base
9+
{
10+
public string Text { get; set; }
11+
12+
[ForeignKey("Content")]
13+
public int ContentId { get; set; }
14+
15+
public virtual Content Content { get; set; }
16+
}
17+
}

src/Example/Data/Entities/User.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations.Schema;
34
using System.Linq;
45
using System.Runtime.Serialization;
56
using System.Threading.Tasks;
@@ -14,7 +15,27 @@ public class User : Base
1415

1516
public string Name { get; set; }
1617

17-
public int? test { get; set; }
18+
public int? Test { get; set; }
19+
20+
[NotMapped]
21+
public string Ignored { get; set; }
22+
23+
[NotMapped]
24+
public string[] VContents
25+
{
26+
get
27+
{
28+
if(Contents != null)
29+
{
30+
return Contents.Select(c => c.Text).ToArray();
31+
}
32+
else
33+
{
34+
return new string[] { };
35+
}
36+
37+
}
38+
}
1839

1940
public virtual List<Content> Contents { get; set; }
2041

src/Example/Program.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public static void Main(string[] args)
2222

2323
watch.Start();
2424

25-
List<User> users = db.Users.ToList(); //db.Users.Include(x => x.Contents).Include(x => x.Settings).ToList();
25+
List<User> users2 = db.Users.Include("Contents.Entries").Include("Contents").Include("Settings").ToList();
26+
27+
28+
List<User> users = db.Users.Include(x => x.Contents).ThenInclude(x => x.Entries).Include(x => x.Settings).ToList(); //db.Users.Include(x => x.Contents).Include(x => x.Settings).ToList();
2629

2730
List<Content> contents = db.Contents.Include(x => x.User).ToList();
2831

@@ -45,10 +48,18 @@ public static void Main(string[] args)
4548

4649
db.SaveChanges();
4750

48-
db.Contents.Add(new Content()
51+
Content c = new Content()
4952
{
5053
Text = "Test",
5154
UserId = us.Id
55+
};
56+
57+
db.Contents.Add(c);
58+
59+
db.ContentEntries.Add(new ContentEntry()
60+
{
61+
Text = "das ist in set",
62+
ContentId = c.Id
5263
});
5364

5465
db.Users.Add(new User()

src/FileContextCore/CombinedManager/ExcelManager.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections;
55
using System.Collections.Generic;
6+
using System.ComponentModel.DataAnnotations.Schema;
67
using System.IO;
78
using System.Linq;
89
using System.Reflection;
@@ -60,7 +61,7 @@ public List<T> GetItems<T>()
6061

6162
if (ws != null)
6263
{
63-
List<PropertyInfo> props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToList();
64+
PropertyInfo[] props = t.GetPropertiesForSerialize();
6465
Dictionary<int, PropertyInfo> properties = new Dictionary<int, PropertyInfo>();
6566

6667
for (int i = 0; i < ws.Dimension.Columns; i++)
@@ -103,7 +104,7 @@ public List<T> GetItems<T>()
103104
{
104105
ws = package.Workbook.Worksheets.Add(t.Name);
105106

106-
PropertyInfo[] props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToArray();
107+
PropertyInfo[] props = t.GetPropertiesForSerialize();
107108

108109
for (int i = 0; i < props.Count(); i++)
109110
{
@@ -143,7 +144,7 @@ public void SaveItems<T>(List<T> list)
143144
}
144145

145146
Type t = list.GetType().GenericTypeArguments[0];
146-
PropertyInfo[] props = t.GetRuntimeProperties().Where(x => !x.SetMethod.IsVirtual).ToArray();
147+
PropertyInfo[] props = t.GetPropertiesForSerialize();
147148

148149
ExcelWorksheet ws = package.Workbook.Worksheets[t.Name];
149150

src/FileContextCore/FileContextCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<RepositoryUrl>https://github.com/morrisjdev/FileContextCore</RepositoryUrl>
2020
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2121
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
22-
<Version>1.0.8</Version>
22+
<Version>1.0.9</Version>
2323
<PackageTags>ef, database, file, db, filedb, json, xml, csv, xlsx, excel, dbcontext, entity framework</PackageTags>
2424
<PackageIconUrl>https://morrisj.net/img/morrisjdev_logo.png</PackageIconUrl>
2525
</PropertyGroup>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
8+
namespace FileContextCore.Helper
9+
{
10+
static class PropertyHelper
11+
{
12+
public static PropertyInfo[] GetPropertiesForSerialize(this Type t)
13+
{
14+
return t.GetRuntimeProperties().Where(x =>
15+
(!x.GetMethod.IsVirtual || x.GetMethod.IsFinal) &&
16+
!x.CustomAttributes.Any(y => y.AttributeType == typeof(NotMappedAttribute)))
17+
.ToArray();
18+
}
19+
20+
public static PropertyInfo[] GetPropertiesNotForSerialize(this Type t)
21+
{
22+
return t.GetRuntimeProperties().Where(x =>
23+
(x.GetMethod.IsVirtual && !x.GetMethod.IsFinal) ||
24+
x.CustomAttributes.Any(y => y.AttributeType == typeof(NotMappedAttribute)))
25+
.ToArray();
26+
}
27+
28+
public static bool ShouldSerialize(this PropertyInfo prop)
29+
{
30+
return (!prop.GetMethod.IsVirtual || prop.GetMethod.IsFinal) &&
31+
!prop.CustomAttributes.Any(y => y.AttributeType == typeof(NotMappedAttribute));
32+
}
33+
}
34+
}

src/FileContextCore/Helper/QueryHelper.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ public static IEnumerable<T> GetValues<T>()
2424
return cache.GetValues<T>();
2525
}
2626

27-
public static IEnumerable<T> LoadRelatedData<T>(IEnumerable<T> values, IncludeSpecification includeSpecification)
27+
private static IList LoadData(IList values, IEnumerable<INavigation> navs)
2828
{
29-
foreach (INavigation nav in includeSpecification.NavigationPath)
29+
INavigation nav = navs.FirstOrDefault();
30+
31+
if (nav != null)
3032
{
3133
IEntityType t;
3234
IClrPropertyGetter[] objProps;
@@ -56,11 +58,11 @@ public static IEnumerable<T> LoadRelatedData<T>(IEnumerable<T> values, IncludeSp
5658

5759
if (nav.IsCollection())
5860
{
59-
foreach (T obj in values)
61+
foreach (object obj in values)
6062
{
6163
object[] objValues = objProps.Select(x => x.GetClrValue(obj)).ToArray();
6264

63-
valueSetter.SetClrValue(obj, cache.Filter(t.ClrType, x =>
65+
IList rValues = cache.Filter(t.ClrType, x =>
6466
{
6567
int result = 0;
6668

@@ -78,12 +80,19 @@ public static IEnumerable<T> LoadRelatedData<T>(IEnumerable<T> values, IncludeSp
7880
}
7981

8082
return result == refValues.Count();
81-
}));
83+
});
84+
85+
if(navs.Count() > 1)
86+
{
87+
rValues = LoadData(rValues, navs.Skip(1));
88+
}
89+
90+
valueSetter.SetClrValue(obj, rValues);
8291
}
8392
}
8493
else
8594
{
86-
foreach (T obj in values)
95+
foreach (object obj in values)
8796
{
8897
object[] objValues = objProps.Select(x => x.GetClrValue(obj)).ToArray();
8998

@@ -109,13 +118,25 @@ public static IEnumerable<T> LoadRelatedData<T>(IEnumerable<T> values, IncludeSp
109118

110119
if (matching.Count > 0)
111120
{
112-
valueSetter.SetClrValue(obj, matching[0]);
121+
object value = matching[0];
122+
123+
if(navs.Count() > 1)
124+
{
125+
value = LoadData(new object[] { value }, navs.Skip(1))[0];
126+
}
127+
128+
valueSetter.SetClrValue(obj, value);
113129
}
114130
}
115131
}
116132
}
117133

118134
return values;
119135
}
136+
137+
public static IEnumerable<T> LoadRelatedData<T>(IEnumerable<T> values, IncludeSpecification includeSpecification)
138+
{
139+
return LoadData(values.ToList(), includeSpecification.NavigationPath).Cast<T>();
140+
}
120141
}
121142
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
4+
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
5+
-->
6+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
7+
<PropertyGroup>
8+
<PublishProtocol>FileSystem</PublishProtocol>
9+
<Configuration>Release</Configuration>
10+
<TargetFramework>netstandard1.6</TargetFramework>
11+
<PublishDir>bin\Release\PublishOutput</PublishDir>
12+
</PropertyGroup>
13+
</Project>

0 commit comments

Comments
 (0)