1
我有幾個實體框架7(核心)的實體:應用包括基於ThenInclude的Lambda表達式字典
public class Person {
public virtual Address Address { get; set; }
public virtual ICollection<Hobby> Hobbies { get; set; }
}
public class Address {
public String Street { get; set; }
public virtual Country Country { get; set; }
}
而且我有一個字符串數組如下:
String[] entities = new String[] { "Hobbies", "Address.Country" }
鑑於此字符串數組我:
context.Persons
.Include(x => x.Hobbies)
.Include(x => x.Address).ThenInclude(x => x.Country);
在EF6我可以這樣做:
context.Persons.Include(entities[0]).Include(entities[1]);
但是在EF7 Include中不允許字符串。我創建了詞典:
private readonly Dictionary<String, LambdaExpression> _properties = new Dictionary<String, LambdaExpression>();
這將是這樣的:
x => x.Hobbies is for "Hobbies"
x => x.Address.Country is for "Address.Country"
和我有擴展名:
public static IQueryable<T> Include<T>(this IQueryable<T> source, Dictionary<String, LambdaExpression> properties) {
}
,我需要給出的解釋適用於以下內容:
Fo R 「X => x.Hobbies」 只是做:
source.Include(x => x.Hobbies);
如果表達式是像 「X => x.Address.Country」 補充:
source.Include(x => x.Address).ThenInclude(x => x.Country);
可以這樣做完了?
在EF7子實體必須包括使用ThenInclude ...這是我的問題之一... –
我做了一個編輯我的答案,再次不確定EF 7,因爲我還沒有使用它,但也許這會幫助你。 –