gpt4 book ai didi

c# - 在 EF Core 中将 IList、List、ICollection 用于 1 到 n 关系

转载 作者:搜寻专家 更新时间:2023-10-30 23:27:15 25 4
gpt4 key购买 nike

<分区>

在 EF Core 中定义一对多关系的最佳做法是什么?

是否建议通常使用列表,因为它们提供更多功能?然后是接口(interface) IList 而不是 List,因为它没有定义实现?如果确实重要,我应该根据什么标准来选择一个或另一个?

Microsoft 建议如下 https://learn.microsoft.com/en-us/ef/core/modeling/relationships

public class Blog
{
public int BlogId { get; set; }
public List<Post> Posts { get; set; }
}

public class Post
{
public int PostId { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}

鉴于

http://www.entityframeworktutorial.net/efcore/configure-one-to-many-relationship-using-fluent-api-in-ef-core.aspx

https://www.learnentityframeworkcore.com/configuration/one-to-many-relationship-configuration建议 ICollections

2011 年的以下答案在 EF Core 中是否仍然有效?

https://stackoverflow.com/a/7655974/10148774

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com