2016-09-19 25 views
2

我正在使用ASP.NET核心和EF核心,我有以下两个父类和子类。每个礼品卡可以有很多交易:如何使用Data Annotations在EF Core迁移中将外键作为NOT NULL?

public class GiftCard 
{ 
    public int Id { get; set; } 
    public string BarCode { get; set; } 
    public DateTime PurchaseDate { get; set; } 
    public string Comments { get; set; } 
    public byte[] Timestamp { get; set; } 
    public List<Transaction.Transaction> Transactions { get; set; } 
} 

public class Transaction 
{ 
    public int Id { get; set; } 
    public DateTime TransactionDate { get; set; } 
    public decimal TransactionAmount { get; set; } 
    public TransactionType TransactionType { get; set; } 
    public byte[] Timestamp { get; set; } 
    public GiftCard.GiftCard GiftCard { get; set; } 
}  

根据我读,这是做,通过对儿童家长和参考导航有导航属性的方式。当我使用命令行添加我的迁移并更新数据库时,除了Transactions表中的GiftCardId外键可为空值外,数据库中的所有内容都可以正常工作。我想确保这不是NULL。我是否缺少数据注解属性?

回答

2

将以下属性放在您的Transaction实体上,应该解决。

public int GiftCardId { get; set; }

什么是你的定义发生的是,正在创建一个影子财产和EF的变化跟踪保持着关系。

请参阅here

相关问题