【发布时间】:2017-08-10 06:44:21
【问题描述】:
我正处于学习阶段。而且我想知道 ASP.NET MVC-5 框架在 Code First 约定中使用实体框架默认为空创建的外键吗?我试图创建外键关系,结果外键列可以为空。我有一个想法,如果我将一个列标记为外键,它应该被实体框架标记为 NOT NULLABLE。
下面是例子:
一对多:一个“标准”可以有多个“子”。
public class Children
{
public int Id { get; set; }
public string Name { get; set; }
public int StandardId { get; set; }
[ForeignKey("StandardId")]
public virtual Standard Standard { get; set; }
}
public class Standard
{
public Standard()
{
Children = new List<Children>();
}
public int Id { get; set; }
public string StandardName { get; set; }
public virtual ICollection<Children> Children { get; set; }
}
通过以上配置,外键“StandardId”在数据库中将变为Nullable。我只想知道这是预期的行为吗?外键是否默认为 NULLABLE 或者我没有正确连接外键属性?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-5 foreign-keys