【发布时间】:2022-01-11 11:26:48
【问题描述】:
我正在观看“CODE FIRST ENTITY FRAMEWORK CORE”的教程我有 2 个问题;
- 我需要使用 [ForeignKey] 属性吗?我应该把它放在哪里,在产品实体中还是在类别实体中(一对一、一对多等都无所谓)
- 为什么没有 [DefaultValue] 属性?
- 关于关系,一些教程显示了这种方式
namespace EntityExample
{
public class Product
{
public int ProductId { get; set; }
public Category Category { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public ICollection<Product> Products { get; set; }
}
}
但其他人喜欢使用它
namespace EntityExample
{
public class Product
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public ICollection<int> ProductId { get; set; }
}
}
哪个是正确的?
【问题讨论】:
-
也许这个文档可以提供帮助:EF Core - Relationships
标签: asp.net-mvc entity-framework entity-framework-core data-annotations table-relationships