我有一个简单的表,其中包含密钥、名称和二进制内容。我只需要在需要时加载二进制内容。这在 Linq2Sql 中曾经非常简单,但在 EF core 6 中,除了导航集合的延迟加载之外,我找不到任何东西,这不是我需要的。我是否遗漏了某些内容,或者 EF core 中缺少此内容吗?吉里
public class Content {
public int Id { get; set; }
public string Name { get; set; }
public byte[] Data { get; set; } // How to delay loading this ?
}
Run Code Online (Sandbox Code Playgroud)
命令
ctx.Content.Select(x =x.Id==1);
Run Code Online (Sandbox Code Playgroud)
预期的 SQL 是:
SELECT Id, Name FROM Content WHERE Id=1
Run Code Online (Sandbox Code Playgroud) 我只能包括相关实体。
using (var context = new BloggingContext())
{
// Load all blogs, all related posts
var blogs1 = context.Blogs
.Include(b => b.Posts)
.ToList();
}
Run Code Online (Sandbox Code Playgroud)
但是,我不需要整个 BlogPost 实体。我只对特定的属性感兴趣,例如:
using (var context = new BloggingContext())
{
// Load all blogs, all and titles of related posts
var blogs2 = context.Blogs
.Include(b => b.Posts.Select(p => p.Title) //throws runtime exeption
.ToList();
foreach(var blogPost in blogs2.SelectMany(b => b.Posts))
{
Console.Writeline(blogPost.Blog.Id); //I need the object graph
Console.WriteLine(blogPost.Title); //writes title
Console.WriteLine(blogPost.Content); //writes null
}
}
Run Code Online (Sandbox Code Playgroud)