[英]Can I get return type from stored procedure without creating DbSet in EF Core?
Supposedly, I have a simple DbContext
with Blog
and Post
models:据说,我有一个带有
Blog
和Post
模型的简单DbContext
:
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public ICollection<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Let's say I have a stored procedure that returns some DTO:假设我有一个返回一些 DTO 的存储过程:
[Keyless]
public class BlogPostDto
{
public string PostTitle { get; init; }
public string BlogName { get; init; }
}
Today I put the following into DbContext
:今天我将以下内容放入
DbContext
:
public class AppDbContext : DbContext {
public virtual DbSet<BlogPostDto> NeverUseIt { get; set; }
partial void OnModelCreatingPartial(ModelBuilder modelBuilder) {
modelBuilder.Entity<BlogPostDto>().ToView(null);
}
}
And then I can get Stored Procedure results shaped in the way I want:然后我可以按照我想要的方式获得存储过程结果:
List<BlogPostDto> results = await db.Set<BlogPostDto>().FromSqlRaw($"EXEC MyProc").ToListAsync();
So, my question is, do I have to add my BlogPostDto
into DbContext
?所以,我的问题是,我是否必须将我的
BlogPostDto
添加到DbContext
? I know that in EF Core 3 I did;我知道在 EF Core 3 中我做到了; but there were a large number of improvements since then.
但从那以后有大量的改进。 Creating a bogus DbSet and mapping it to non-existent view just feels counter-intuitive!
创建一个虚假的 DbSet 并将其映射到不存在的视图只是感觉违反直觉!
The closest I found in most current documentation is here .我在最新文档中找到的最接近的是这里。 The very first example of context is Serving as the return type for raw SQL queries.
上下文的第一个示例是作为原始 SQL 查询的返回类型。 - but the article assumes that I have a matching view already in the database.
- 但本文假设我在数据库中已经有一个匹配的视图。
UPDATE : It looks like ToView(null)
is not necessary - just DbSet<>
更新:看起来
ToView(null)
- 只是DbSet<>
Nothing has changed in that regard so far from what you see in the EF Core 6.0 documentation and SO posts you are referring to.到目前为止,与您在 EF Core 6.0 文档和您所指的 SO 帖子中看到的内容相比,这方面没有任何变化。
Just to be crystal clear, you don't need a DbSet<T>
returning property in your context.为了清楚起见,您不需要
DbSet<T>
在您的上下文中返回属性。 But you do need to include the type (keyless or not) in the model using the modelBuilder.Entity<T>()
call, and also ToView(null)
to prevent EF Core migrations associate database table and/or view with it, and optionally HasNoKey()
in case you don;t want to use EF Core dependent attributes like [Keyless]
in your data classes.但是您确实需要使用
modelBuilder.Entity<T>()
调用在 model 中包含类型(无论是否无密钥),以及ToView(null)
以防止 EF Core 迁移将数据库表和/或视图与其关联,并且可选HasNoKey()
以防您不想在数据类中使用 EF Core 相关属性,例如[Keyless]
。
So the minimum requirement for your example is this line所以你的例子的最低要求是这一行
modelBuilder.Entity<BlogPostDto>().ToView(null);
Now, this is a long time requested feature (which exists in the "obsolete" EF6 which the "modern" EF Core is supposed to replace), tracked by Support raw SQL queries without defining an entity type for the result #10753 issue in EF Core issue tracker.现在,这是一个长期请求的功能(它存在于“现代”EF Core 应该替换的“过时”EF6 中),由支持原始 SQL 查询跟踪,但没有为 EF 中的结果 #10753 问题定义实体类型核心问题跟踪器。 It was initially planned to be included in the upcoming EF Core 7.0 release (Nov 2022), but later has been cut for (eventually) EF Core 8.0 (Nov 2023).
它最初计划包含在即将发布的 EF Core 7.0版本(2022 年 11 月)中,但后来(最终)在 EF Core 8.0(2023 年 11 月)中被删减。 So until then you have to use the "register model" approach, or use 3rd party library(,) like Dapper for the same task?
所以在那之前你必须使用“注册模型”方法,或者使用像 Dapper 这样的 3rd 方库(,)来完成相同的任务? as suggested by one of the EF Core team members(.!).
正如 EF Core 团队成员之一(.!)所建议的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.