我是实体框架的新手。我试图建立两个对象之间的关系,但是这样做似乎有些问题。
我有两个实体,分别是餐厅和支票。一个餐厅可以有很多支票,但是一张支票仅属于单间餐厅。
[Table("Check")]
public class Check
{
[Key]
public Guid Id { get; set; }
public DateTime OpenDate { get; set; }
public DateTime? CloseDate { get; set; }
public decimal Amount { get; set; }
public Guid EposCheckId { get; set; }
public decimal TipAmount { get; set; }
public int TableNumber { get; set; }
public Guid StaffId { get; set; }
[ForeignKey("RestaurantId")]
public Guid RestaurantId { get; set; }
public virtual Staff memberOfStaff { get; set; }
public virtual Restaurant Restaurant { get; set; }
public virtual ICollection<Payment> Payments { get; set; }
//public ICollection<SalesEntry> SalesEntries { get; set; }
public string ToJson()
{
return JsonConvert.SerializeObject(this);
}
}
[Table("Restaurant")]
public class Restaurant
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public string ContactName { get; set; }
public string Email { get; set; }
public string LoginId { get; set; }
public string Pin { get; set; }
public string EposAuthToken { get; set; }
public int EposBusinessId { get; set; }
public long EposBusinessLocationId { get; set; }
public TimeSpan DayEndTime { get; set; }
public bool Enabled { get; set; }
public string RegistrationStatus { get; set; }
public byte[] Logo { get; set; }
public string LogoContentType { get; set; }
public string UserName { get; set; }
public string PasswordHash { get; set; }
public string MerchantIdentifier { get; set; }
public string MerchantSecretKey { get; set; }
public virtual ICollection<Check> Checks { get; set; }
}
在一段代码中,我检索了一张支票,然后试图获取该支票所属餐厅的ID,但这样做时出现以下错误;
System.NullReferenceException: Object reference not set to an instance of an object.
即使支票上的'RestaurantId'属性可用,支票上的'Restaurant'属性仍为空。
我在做什么错了?