在 net core MVC 中使用 objects 列表

Working with list of objects in net core MVC

我有一些关于在模型中使用列表的问题。

简而言之: 我有继承自 IdentityUser 的模型用户。有一些额外的字段和 objects 的列表(可以说是 child 的列表)所以

public List<Child> Childs

现在我不知道列表是如何存储在数据库中的,是吗? 没有列 Childs。它是否仅用于存储从数据库中获取的数据?

我的问题是我不知道如何使用它。可以说我想显示来自已登录用户的所有 child。我是否循环遍历所有 childs,找到哪些具有用户 ID,将其移至列表,然后 foreach 循环它?或者我应该直接访问 Childs 列表而不先从数据库中获取数据? 但是如果我更改一些 child 的数据会发生什么? 假设我向 1 个用户添加了 2 childs,稍后更改单个 child 的数据,这样 child 的数据不会在列表中更改,我需要在每个列表?我相信不是这样处理的。

希望我能得到一些关于列表的解释。

Now I don't know how lists are stored in database, are they tho? There is no column Childs. Is it used only to store data taken from DB?

通常记录在单独的 table 中,例如 UserChild,它引用父 table

Lets say I want to display all Childs from logged user.

var allChildren = ctx.Users
   .Where(u => u.Id == userId)
   .SelectMany(u => u.Childs)
   .ToList();

But then what happens if I change data of some childs? Lets say I added 2 childs to 1 user, later change data of single child, that way data of this child won't change in list and i need to change it in every list? I believe that's not how it's handled.

更改应记录在适当的 table 中,上面的查询应从数据库中获取更新的数据。

可能在开始做某事之前,您应该阅读 EF Core 文档。