【问题标题】:List of elements in a model in .net core.net core 中模型中的元素列表
【发布时间】:2021-12-14 04:22:18
【问题描述】:

我试图了解模型在 .net 核心中的工作原理,但遇到了以下问题: 我有一个名为 blog 的模型:

public class Blog
{
    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "Title is required")]
    [StringLength(40)]
    public string Title { get; set; }

    [Required(ErrorMessage = "Date is required")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime Date { get; set; }

    [Required(ErrorMessage = "Image is required")]
    public string Image { get; set; }

    [Required(ErrorMessage = "Description is required")]
    [StringLength(400)]
    public string Description { get; set; }

    public virtual IList<Paragraph> Paragraphs { get; set; } = new List<Paragraph>();

    [Required(ErrorMessage = "IsActive is required")]
    public bool IsActive { get; set; }

    [Required]
    public int? CompanyId { get; set; }
    public Company Company { get; set; }

    public Blog(){}
}

还有段落模型:

    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "Section is required")]
    public string Section { get; set;}

    [Required]
    public int? BlogId { get; set; }
    public Blog Blog { get; set; }

    public Paragraph()
    {
    }

当执行应该从这个博客给我带来数据的方法时,它会返回空段落列表

    [HttpGet]
    [Route("api/[controller]/{id}")]
    public IActionResult GetById(int id)
    {
        var findBlog = _db.Blog.Find(id);
        if (findBlog == null)
        {
            return NotFound("Blog not found");
        }
        return new JsonResult(findBlog);
    }

回应:

{
"id": 6,
"title": "Test List",
"date": "2021-10-28T00:00:00",
"image": "https://images.pexels.com/photos/2489/street-building-construction-industry.jpg?auto=compress&cs=tinysrgb&dpr=2&w=500",
"description": "Desc",
"paragraphs": [],
"isActive": true,
"companyId": 1,
"company": null
}

【问题讨论】:

  • 我相信您在控制器中签入的结果是您得到了正确的列表,只有转换问题。我注意到两件事。首先尝试将virtual IList&lt;Paragraph&gt; 更改为List&lt;Paragraph&gt; 即普通列表。第二个var findBlog = _db.Blog.Find(id);List&lt;blog&gt; findBlog 转换以获取/检查精确映射
  • 非常感谢,这帮我解决了另一个问题

标签: asp.net .net asp.net-core .net-core


【解决方案1】:

我想您正在使用 Entity Framework Core。然后你需要load related data使用include函数。

举个例子

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Include(blog => blog.Posts)
        .Include(blog => blog.Owner)
        .ToList();
}

适合你的情况

 await _dbContext.Blogs
 .Include(blog => blog.Paragraphs)
 .FirstOrDefaultAsync(blog => blog.Id == id);

为了实现这一点,您需要在实体之间建立适当的关系。

查看 SchoolContext here

【讨论】:

  • 我使用的是 .net core 3.1
  • 是的,efcore 应该在那里执行数据库调用。它正在.net core 中处理数据库操作。
【解决方案2】:

您可以尝试使用.Inclue() 包含如下相关数据:

using Microsoft.EntityFrameworkCore;

var findBlog = _db.Blog.Include(a => a.Paragraphs).First(a => a.Id == id);
// all these ways can work well
//var findBlog = _db.Blog.Include(a => a.Paragraphs).Where(a => a.Id == id).ToList();
//var findBlog = _db.Blog.Include(a => a.Paragraphs).FirstOrDefault(a => a.Id == id);

参考:

Eager Loading of Related Data

【讨论】:

  • 在此之后 Include 无法识别我
  • 我没有导入EntityFrameworkCore,我会运行一个测试,我想这应该是我的问题
  • 好的,您可以使用我的代码进行测试,我会等待您的回复。如果有任何问题,请跟进让我知道。如果我的回答对您有帮助,请记住接受作为答案。谢谢。
  • 请查看我更新的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多