我正在尝试使用Entity Framework代码优先方法向数据库中添加一些数据。
这是我的代码:
Member member = new Member
{
...
Cards = new List<Card>() {new Card { Number = model.CartBankNumber, RegisterTime = DateTime.Now, Status = CardStatus.Verifying } },
};
db.Member.Add(member);
await db.SaveChangesAsync();
这是我的Member
模型班:
public class Member
{
...
public Member()
{
Cards = new List<Card>();
}
[Display(Name = "Cards", ResourceType = typeof(Resources))]
public virtual ICollection<Card> Cards{ get; set; }
}
和Card
模型类:
public class Card
{
...
[Display(Name = "Id", ResourceType = typeof(Resources))]
public int Id { get; set; }
[ForeignKey("Member")]
public string MemberId { get; set; }
public virtual Member Member { get; set;}
}
为什么MemberId
为空?