实体框架 - 列表(MVC)
public class Author
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Book> Books { get; set; } = new List<Book>();
};
public class Book
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int AuthorId { get; set; }
public Author? author { get; set; }
};
我的代码与此类似,我可以在作者控制器列表中分配给作者。书籍还是仅用于关系的属性?
例如,
author.Books = await _context.Books.Where(b => b.AuthorId == Id).ToListAsync();
它是正确的代码还是创建未图表的属性?
public class Author
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Book> Books { get; set; } = new List<Book>();
};
public class Book
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int AuthorId { get; set; }
public Author? author { get; set; }
};
I have code similar to this, can I assign in author controller list of books to author.Books or it's property only for relation?
For example
author.Books = await _context.Books.Where(b => b.AuthorId == Id).ToListAsync();
It's correct code or create NotMapped property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以获取带有作者查询的书籍,并且无需
使用
include()
这样的书来获取新书以获取书籍:you can get books with author query and there is no need to make a new one to get books
by using
Include()
like this :