Entity framework core 在带有数据注释的EF Core迁移中,如何使外键不为NULL?
我使用ASP.NET Core和EF Core,我有以下两个父类和子类。每张礼品卡可以有许多交易:Entity framework core 在带有数据注释的EF Core迁移中,如何使外键不为NULL?,entity-framework-core,entity-framework-migrations,Entity Framework Core,Entity Framework Migrations,我使用ASP.NET Core和EF Core,我有以下两个父类和子类。每张礼品卡可以有许多交易: 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[
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; }
}
公共类礼品卡
{
公共int Id{get;set;}
公共字符串条形码{get;set;}
public DateTime PurchaseDate{get;set;}
公共字符串注释{get;set;}
公共字节[]时间戳{get;set;}
公共列表事务{get;set;}
}
公共类事务
{
公共int Id{get;set;}
公共日期时间事务日期{get;set;}
公共十进制事务装入{get;set;}
公共事务类型事务类型{get;set;}
公共字节[]时间戳{get;set;}
public GiftCard.GiftCard GiftCard{get;set;}
}
根据我读到的内容,这是一种方法,在父级上具有导航属性,在子级中具有引用导航。当我添加迁移并使用命令行更新数据库时,数据库中的一切似乎都正常,只是事务表中的giftcarid外键可以为null。我想确保这不是空的。我是否缺少数据注释属性?将以下属性放在您的
事务
实体上,应该解决它
public int GiftCardId{get;set;}
您的定义所发生的情况是,正在创建一个阴影属性,EF的更改跟踪器正在维护这些关系
请参阅。该页面不存在于您提供的[必需]属性的超链接中