下面是我的代码:
我在ObjectQuery里include对应外键所在的表,这样起不了左右,对应的表还是null。我知道用VsleaDataEntities.Customers.include("aa");是可以做到的,但我的目的仅仅是为了通用,或者我可以用InvokeMember来做到?
1 private TEntity GetEntityWithReferenceLoadedById<TEntity>(object entityId)
2 {
3 TEntity entity = default(TEntity);
4
5 var query = string.Format("SELECT VALUE Entity FROM {0}.{1} AS Entity "
6 + " WHERE Entity.Id = @Id", GetDataEntitiesContainerName(), typeof(TEntity).Name);
7
8
9 ObjectQuery<TEntity> objectQuery = new ObjectQuery<TEntity>(query, ObjectContext, MergeOption.NoTracking);
10
11 objectQuery.Parameters.Add(new ObjectParameter("Id", entityId));
12
13 List<string> includePathList = this.GetIncludePathList(typeof(TEntity));
14 foreach (string path in includePathList)
15 {
16 objectQuery = objectQuery.Include(path);
17 }
18
19 entity = objectQuery.FirstOrDefault();
20
21 return entity;
22 }
2 {
3 TEntity entity = default(TEntity);
4
5 var query = string.Format("SELECT VALUE Entity FROM {0}.{1} AS Entity "
6 + " WHERE Entity.Id = @Id", GetDataEntitiesContainerName(), typeof(TEntity).Name);
7
8
9 ObjectQuery<TEntity> objectQuery = new ObjectQuery<TEntity>(query, ObjectContext, MergeOption.NoTracking);
10
11 objectQuery.Parameters.Add(new ObjectParameter("Id", entityId));
12
13 List<string> includePathList = this.GetIncludePathList(typeof(TEntity));
14 foreach (string path in includePathList)
15 {
16 objectQuery = objectQuery.Include(path);
17 }
18
19 entity = objectQuery.FirstOrDefault();
20
21 return entity;
22 }