小编Vla*_*ins的帖子

实体框架 - 延迟加载或额外的异步/等待查询方法?

我有这些域模型

public class Topic
{
    public int TopicId { get; set; }

    public virtual ICollection<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }

    public int? TopicId { get; set; }
    public virtual Topic Topic { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

例如,我想暗示方法TestAsync,我想使用Topic对象和相关的Posts对象.

主题模型我可以使用async方法和topicId作为param.

public async Task<bool> TestAsync(int topicId)
{
    var topic = await topicService.GetByIdAsync(topicId);

    // posts ...
}
Run Code Online (Sandbox Code Playgroud)

我有两种方法,如何获得相关的帖子.但是,如果我将使用LazyLoading或只是另一个异步查询,有什么区别?

// Example: 1 (LazyLoading)
var posts = topic.Posts;

// OR Example: 2 (Async method) …
Run Code Online (Sandbox Code Playgroud)

c# asp.net entity-framework lazy-loading async-await

5
推荐指数
1
解决办法
2122
查看次数