/
EntityLoader.cs
88 lines (80 loc) · 3.11 KB
/
EntityLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using ServiceInterfaces;
namespace DataModeling
{
public class EntityLoader<TEntity> : IEntityLoader<TEntity>
where TEntity : class
{
private readonly DbContext context;
private readonly TEntity entity;
internal EntityLoader(DbContext context, TEntity entity)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (entity == null)
{
throw new ArgumentNullException("entity");
}
IObjectContextAdapter adapter = context;
var objectContext = adapter.ObjectContext;
var workspace = objectContext.MetadataWorkspace;
EntityType entityType;
if (!workspace.TryGetItem<EntityType>(typeof(TEntity).FullName, DataSpace.OSpace, out entityType))
{
const string format = "The entity type {0} is not part of the model for the current context."
+ " Verify the entity is configured with the context and not a complex type.";
string message = String.Format(null, format, typeof(TEntity).Name);
throw new InvalidOperationException(message);
}
this.context = context;
this.entity = entity;
}
public void Reload()
{
DbEntityEntry<TEntity> entry = context.Entry(entity);
entry.Reload();
}
public void Load<TRelation>(Expression<Func<TEntity, TRelation>> accessor)
where TRelation : class
{
DbEntityEntry<TEntity> entry = context.Entry(entity);
DbReferenceEntry reference = entry.Reference(accessor);
if (!reference.IsLoaded)
{
reference.Load();
}
}
public void Load<TRelation>(Expression<Func<TEntity, ICollection<TRelation>>> accessor)
where TRelation : class
{
DbEntityEntry<TEntity> entry = context.Entry(entity);
DbCollectionEntry collection = entry.Collection(accessor);
if (!collection.IsLoaded)
{
collection.Load();
}
}
public IQueryable<TRelation> LoadQuery<TRelation>(Expression<Func<TEntity, TRelation>> accessor)
where TRelation : class
{
DbEntityEntry<TEntity> entry = context.Entry(entity);
DbReferenceEntry<TEntity, TRelation> reference = entry.Reference(accessor);
return reference.Query();
}
public IQueryable<TRelation> LoadQuery<TRelation>(Expression<Func<TEntity, ICollection<TRelation>>> accessor)
where TRelation : class
{
DbEntityEntry<TEntity> entry = context.Entry(entity);
DbCollectionEntry<TEntity, TRelation> collection = entry.Collection(accessor);
return collection.Query();
}
}
}