c# - EF Core 获取整个对象以进行空检查
问题描述
我有Book
和Author
域类,以及BookDto
作为AuthorDto
它们各自域类的子选择的类。
在应用程序的某个地方,我必须公开一个IQueryable<BookDto>
而不是一个IQueryable<Book>
,所以,我编写了以下代码来进行转换,没什么复杂的:
var queryableDto = dbContext.Books.Select(book => new BookDto
{
Name = book.Name,
...
Author = book.Author == null ? null : new AuthorDto
{
Id = book.Author.Id,
Name = book.Author.Name,
...
}
});
然后我做了这个:
var dtos = queryableDto.Select(bDto => new BookDto
{
Name = bDto.Name,
Author = bDto.Author == null ? null : new AuthorDto() { Name = bDto.Author.Name }
}).ToList();
但是我的第二个查询中的空值检查存在一个大问题(它说Author = bDto.Author == null ? null : new...
)。因为当我说:bDto.Author == null
发生的事情是实体框架从数据库中检索整个 Author 对象及其所有列,但这显然不应该是这种情况,这不是我打算做的。当您使用IQueryable
域类(如在第一个代码块中)时,这些空检查会被转换为 SQL,但在您执行此类操作时不会。
我也无法摆脱空检查,因为我必须找出这本书是否有作者。(我知道在现实生活中不可能有一本书没有作者,但这只是一个例子)
那么,真的有办法摆脱这种情况吗?我如何告诉 Entity Framework,当我说bDto.Author == null
检索整个事情并不是我真正想要做的事情时?
解决方案
所以你在 Authors 和 Books 之间有一对多的关系:每个 Author 写了零个或多个 Books,每个 Book 都是由一个 Author 写的,即外键 AuthorId 引用的 Author。
我使用完整的实体框架。对于您的问题,我将使用 Book 的虚拟属性。实体框架知道作者和书籍之间的关系,并将为您创建正确的(组)连接。
我听说实体框架的核心版本在使用虚拟属性方面有一些限制,尽管我从未检查过它们。因此,我将为您提供使用虚拟属性的解决方案,以及您自己进行加入的解决方案。
使用虚拟属性。
如果您遵循实体框架约定,您的类将类似于以下内容:
class Author
{
public int Id {get; set;}
public string Name {get; set;}
...
// Every Author wrote zero or more Books (one-to-many)
public virtual ICollection<Book> Books {get; set;}
}
class Book
{
public int Id {get; set;}
public string Name {get; set;}
...
// Every Book has been written by exactly one Author, using foreign key
public int AuthorId {get; set;}
public virtual Author Author {get; set; }
}
解决方案可能如下所示:
public MyDbContext : DbContext
{
public DbSet<Author> Authors {get; set;}
public DbSet<Book> Books {get; set;}
public IQueryable<BookDto> BookDtos => this.Books.Select(book => new BookDto
{
Id = book.Id,
Name = book.Name,
...
// A BookDto has exactly one Author
Author = new AuthorDto
{
Id = book.Author.Id,
Name = book.Author.Name,
...
},
});
}
如果要强制使用 BookDtos 而不是 Books,可以将查询包装在接口中,并提供接口而不是完整的 MyDbContext。
自己加入
IQueryable<BookDto> BookDtos => this.Books.Join(
this.Authors,
book => book.AuthorId, // from every book take the foreign key to Author
author => author.BookId, // from every Author take the primary key
// parameter resultSelector: for every Book with its one-and-only Author,
// make one new BookDto:
(book, author) => new BookDto
{
Id = book.Id,
Name = book.Name,
...
Author = new AuthorDto
{
Id = book.Author.Id,
Name = book.Author.Name,
...
},
});
如果您想获得作者的书籍:
IQueryable<AuthorDto> AuthorDtos => this.Authors.GroupJoin(
this.Books,
author = author.Id, // from every Author take the primary key
book = book.AuthorId, // from every Book take the foreign key to Author
// parameter resulSelector: take every author with his zero or more Books
// to create one new AuthorDto:
(author, booksOfThisAuthor) => new AuthorDto
{
Id = author.Id,
Name = author.Name,
...
Books = booksOfThisAuthor.Select(book => new BookDto
{
Id = book.Author.Id,
Name = book.Author.Name,
...
}),
});
如果在一对多关系中,您希望“具有零个或多个子项的项目”使用 GroupJoin 并从一侧开始。如果您想要“具有外键引用的唯一项目的项目”,请使用 Join 并从多方面开始。
如果您使用参数 resultSelector 检查 Queryable.GroupJoin 的重载,您会看到它booksOfThisAuthor
的类型为System.Collections.Generic.IEnumerable<TInner>,TResult>>
。所以不是IQueryable<...>
。因此,如果您希望用户不想要所有 Books,或者不想要完整的 BookDto,我不确定这种方法是否有效。
推荐阅读
- testng - 如何将 TestNG @DataProvider 迁移到 JUnit Jupiter @ParameterizedTest
- html - 检查输入时如何使列表项可见
- java - 在 spring 控制器中获取请求路径
- python - 在 Python 中使用 __str__ 打印对象时遇到问题
- javascript - Babel 不处理 node_modules - 没有排除,没有 .babelrc
- vb.net - 用三个列表框的值编写一个 .txt
- azure-functions - 无法加载文件或程序集“Microsoft.Extensions.Logging.Abstractions
- html - COREUI 侧边栏最小化器和下拉菜单不起作用
- python-3.x - 尝试将 tf-nightly-gpu 与 RTX 30 卡一起使用
- javascript - 如何正确映射一个数组,该数组来自我在 mapStateToProps 中定义的属性之一,以便在我的表单中选择下拉菜单?