代码如下所示:
namespace Test
{
public interface IMyClass
{
List<IMyClass> GetList();
}
public class MyClass : IMyClass
{
public List<IMyClass> GetList()
{
return new List<IMyClass>();
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码分析时,我得到以下建议.
警告3 CA1002:Microsoft.Design:更改'IMyClass.GetList()'中的'List'以使用Collection,ReadOnlyCollection或KeyedCollection
我应该如何解决这个问题以及这里有什么好的做法?
这是这里解释的问题的第二步:EF 4.1代码优先:如何加载相关数据(父子孙子)?.在@Slauma的指导下,我使用这种方法成功检索了数据:
var model = DbContext.SitePages
.Where(p => p.ParentId == null && p.Level == 1)
.OrderBy(p => p.Order) // ordering parent
.ToList();
foreach (var child in model) { // loading children
DbContext.Entry(child)
.Collection(t => t.Children)
.Query()
.OrderBy(t => t.Order) // ordering children
.Load();
foreach (var grand in child.Children) { // loading grandchildren
DbContext.Entry(grand)
.Collection(t => t.Children)
.Query()
.OrderBy(t => t.Order) // ordering grandchildren
.Load();
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这种方法有效,但它会向数据库发送许多查询,而我正在寻找一种方法,只需一个查询即可完成所有操作.在@Slauma的指导下(在上面链接的答案中解释),我已经将查询更改为:
var model2 = DbContext.SitePages
.Where(p => p.ParentId …
Run Code Online (Sandbox Code Playgroud) entity-framework parent-child ef-code-first entity-framework-4.1