Skip to content

Commit 407f2b7

Browse files
Merge pull request MikaelEliasson#11 from dahrnsbrak/patch-1
MappingHelper.cs for Type Hierarchies
2 parents bb14ca1 + 6c259aa commit 407f2b7

File tree

8 files changed

+114
-11
lines changed

8 files changed

+114
-11
lines changed

EntityFramework.Utilities/EntityFramework.Utilities/EFQueryHelpers.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Data.Entity;
44
using System.Data.Entity.Core.Metadata.Edm;
5+
using System.Data.Entity.Core.Objects;
56
using System.Data.Entity.Infrastructure;
67
using System.Linq;
78
using System.Linq.Expressions;
@@ -45,13 +46,14 @@ public static EFUQueryable<T> IncludeEFU<T, TChild>(this IQueryable<T> query, Db
4546
var e = new IncludeExecuter<T>
4647
{
4748
ElementType = typeof(TChild),
48-
SingleItemLoader = (parent) => {
49+
SingleItemLoader = (parent) =>
50+
{
4951
if (parent == null)
5052
{
5153
return;
5254
}
5355
var children = octx.CreateObjectSet<TChild>();
54-
var lambdaExpression = GetRootEntityToChildCollectionSelector<T,TChild>(cSpaceType);
56+
var lambdaExpression = GetRootEntityToChildCollectionSelector<T, TChild>(cSpaceType);
5557

5658
var q = ApplyChildCollectionModifiers<TChild>(children, childCollectionModifiers);
5759

@@ -64,10 +66,18 @@ public static EFUQueryable<T> IncludeEFU<T, TChild>(this IQueryable<T> query, Db
6466
q = q.AsNoTracking().Where(where);
6567

6668
setter((T)parent, q.ToList());
67-
},
69+
},
6870
Loader = (rootFilters, parents) =>
6971
{
70-
var set = octx.CreateObjectSet<T>();
72+
var baseType = typeof(T).BaseType ?? typeof(T);
73+
74+
dynamic dynamicSet = octx.GetType()
75+
.GetMethod("CreateObjectSet", new Type[] { })
76+
.MakeGenericMethod(baseType)
77+
.Invoke(octx, new Object[] { });
78+
79+
var set = dynamicSet.OfType<T>() as ObjectQuery<T>;
80+
7181
IQueryable<T> q = set;
7282
foreach (var item in rootFilters)
7383
{
@@ -77,7 +87,7 @@ public static EFUQueryable<T> IncludeEFU<T, TChild>(this IQueryable<T> query, Db
7787
q = q.Provider.CreateQuery<T>(newMethods);
7888
}
7989

80-
var lambdaExpression = GetRootEntityToChildCollectionSelector<T,TChild>(cSpaceType);
90+
var lambdaExpression = GetRootEntityToChildCollectionSelector<T, TChild>(cSpaceType);
8191

8292
var childQ = q.SelectMany(lambdaExpression);
8393
childQ = ApplyChildCollectionModifiers<TChild>(childQ, childCollectionModifiers);

EntityFramework.Utilities/EntityFramework.Utilities/MappingHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public EfMapping(DbContext db)
121121
.Descendants()
122122
.Single(e =>
123123
e.Name.LocalName == "EntityTypeMapping"
124-
&& e.Attribute("TypeName").Value == set.ElementType.FullName)
124+
&& (e.Attribute("TypeName").Value == set.ElementType.FullName || e.Attribute("TypeName").Value == string.Format("IsTypeOf({0})", set.ElementType.FullName)))
125125
.Descendants()
126126
.Where(e => e.Name.LocalName == "MappingFragment");
127127

EntityFramework.Utilities/Tests/FakeDomain/Contact.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55

66
namespace Tests.FakeDomain
77
{
8-
public class Contact
8+
public class Contact : Person
99
{
10-
public Guid Id { get; set; }
11-
public string FirstName { get; set; }
12-
public string LastName { get; set; }
13-
public DateTime BirthDate { get; set; }
10+
public string Title { get; set; }
1411
public ICollection<PhoneNumber> PhoneNumbers { get; set; }
1512
public ICollection<Email> Emails { get; set; }
1613
}

EntityFramework.Utilities/Tests/FakeDomain/Context.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ private Context(string connectionString)
1717
}
1818

1919
public DbSet<BlogPost> BlogPosts { get; set; }
20+
public DbSet<Person> People { get; set; }
2021
public DbSet<Contact> Contacts { get; set; }
2122
public DbSet<PhoneNumber> PhoneNumbers { get; set; }
2223
public DbSet<Email> Emails { get; set; }
@@ -27,6 +28,10 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
2728
base.OnModelCreating(modelBuilder);
2829
modelBuilder.ComplexType<AuthorInfo>();
2930
modelBuilder.ComplexType<Address>();
31+
32+
//Table per Type Hierarchy setup
33+
modelBuilder.Entity<Person>().ToTable("Person");
34+
modelBuilder.Entity<Contact>().ToTable("Contact");
3035
}
3136

3237
public static Context Sql()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tests.FakeDomain
7+
{
8+
public class Person
9+
{
10+
public Guid Id { get; set; }
11+
public string FirstName { get; set; }
12+
public string LastName { get; set; }
13+
public DateTime BirthDate { get; set; }
14+
}
15+
}

EntityFramework.Utilities/Tests/IncludeTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ private static void CreateSmallTestSet(Context db)
475475
{
476476
FirstName = "FN1",
477477
LastName = "LN1",
478+
Title = "Director",
478479
Id = Guid.NewGuid(),
479480
BirthDate = DateTime.Today,
480481
PhoneNumbers = new List<PhoneNumber>(){
@@ -492,6 +493,7 @@ private static void CreateSmallTestSet(Context db)
492493
{
493494
FirstName = "FN2",
494495
LastName = "LN2",
496+
Title = "Associate",
495497
Id = Guid.NewGuid(),
496498
BirthDate = DateTime.Today,
497499
PhoneNumbers = new List<PhoneNumber>(){
@@ -514,6 +516,7 @@ private static void CreateSmallTestSet(Context db)
514516
{
515517
FirstName = "FN3",
516518
LastName = "LN3",
519+
Title = "Vice President",
517520
Id = Guid.NewGuid(),
518521
BirthDate = DateTime.Today,
519522
Emails = new List<Email>()

EntityFramework.Utilities/Tests/InsertTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,84 @@
44
using Microsoft.VisualStudio.TestTools.UnitTesting;
55
using Tests.FakeDomain;
66
using Tests.FakeDomain.Models;
7+
using System;
78

89
namespace Tests
910
{
1011
[TestClass]
1112
public class InsertTests
1213
{
14+
[TestMethod]
15+
public void InsertAll_InsertItems_WithTypeHierarchy()
16+
{
17+
using (var db = Context.Sql())
18+
{
19+
if (db.Database.Exists())
20+
{
21+
db.Database.Delete();
22+
}
23+
db.Database.Create();
24+
25+
List<Contact> people = new List<Contact>();
26+
people.Add(new Contact
27+
{
28+
FirstName = "FN1",
29+
LastName = "LN1",
30+
Title = "Director",
31+
Id = Guid.NewGuid(),
32+
BirthDate = DateTime.Today,
33+
PhoneNumbers = new List<PhoneNumber>(){
34+
new PhoneNumber{
35+
Id = Guid.NewGuid(),
36+
Number = "10134"
37+
},
38+
new PhoneNumber{
39+
Id = Guid.NewGuid(),
40+
Number = "15678"
41+
},
42+
}
43+
});
44+
people.Add(new Contact
45+
{
46+
FirstName = "FN2",
47+
LastName = "LN2",
48+
Title = "Associate",
49+
Id = Guid.NewGuid(),
50+
BirthDate = DateTime.Today,
51+
PhoneNumbers = new List<PhoneNumber>(){
52+
new PhoneNumber{
53+
Id = Guid.NewGuid(),
54+
Number = "20134"
55+
},
56+
new PhoneNumber{
57+
Id = Guid.NewGuid(),
58+
Number = "25678"
59+
},
60+
},
61+
Emails = new List<Email>()
62+
{
63+
new Email{Id = Guid.NewGuid(), Address = "[email protected]" },
64+
new Email{Id = Guid.NewGuid(), Address = "[email protected]" },
65+
}
66+
});
67+
people.Add(new Contact
68+
{
69+
FirstName = "FN3",
70+
LastName = "LN3",
71+
Title = "Vice President",
72+
Id = Guid.NewGuid(),
73+
BirthDate = DateTime.Today,
74+
Emails = new List<Email>()
75+
{
76+
new Email{Id = Guid.NewGuid(), Address = "[email protected]" },
77+
new Email{Id = Guid.NewGuid(), Address = "[email protected]" },
78+
}
79+
});
80+
81+
EFBatchOperation.For(db, db.People).InsertAll(people);
82+
}
83+
}
84+
1385
[TestMethod]
1486
public void InsertAll_InsertsItems()
1587
{

EntityFramework.Utilities/Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="AttachAndModifyTest.cs" />
7777
<Compile Include="FakeDomain\Contact.cs" />
7878
<Compile Include="FakeDomain\Email.cs" />
79+
<Compile Include="FakeDomain\Person.cs" />
7980
<Compile Include="FakeDomain\PhoneNumber.cs" />
8081
<Compile Include="FakeDomain\Queries.cs" />
8182
<Compile Include="IncludeTest.cs" />

0 commit comments

Comments
 (0)