实体框架 6 包括在 null 中检索数据 - 一对一关系
百货商店模型。他们是一对一的关系。当我仅执行(包含)某些模型时,它会为我带来完整的数据。 中的数据正确
var orders = await _context.Order.Include(o => o.Departament)
.Include(o => o.Store).ToArrayAsync();
数据库Model Order
public class Order
{ [Key]
public string Order_Id { get; set; }
[DataType(DataType.Date)]
public DateTime Date { get; set; }
public string Number_Order { get; set; }
public int Qty { get; set; }
[DataType(DataType.DateTime)]
public DateTime Imported { get; set; }
[DataType(DataType.DateTime)]
public DateTime Modified { get; set; }
[DataType(DataType.DateTime)]
public DateTime Exported { get; set; }
public bool IsActive { get; set; }
[ForeignKey("Departament")]
public string DepartmentId { get; set; }
[ForeignKey("Store")]
public string StoreId { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
public Departament Departament { get; set; }
public Store Store { get; set; }
}
。
我只需要解决正确获取数据的问题。
Department and store model. They are a one to one relationship. when I do the (Include) only some models it brings me the complete data.
The data is correctly in the database
var orders = await _context.Order.Include(o => o.Departament)
.Include(o => o.Store).ToArrayAsync();
Model Order
public class Order
{ [Key]
public string Order_Id { get; set; }
[DataType(DataType.Date)]
public DateTime Date { get; set; }
public string Number_Order { get; set; }
public int Qty { get; set; }
[DataType(DataType.DateTime)]
public DateTime Imported { get; set; }
[DataType(DataType.DateTime)]
public DateTime Modified { get; set; }
[DataType(DataType.DateTime)]
public DateTime Exported { get; set; }
public bool IsActive { get; set; }
[ForeignKey("Departament")]
public string DepartmentId { get; set; }
[ForeignKey("Store")]
public string StoreId { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
public Departament Departament { get; set; }
public Store Store { get; set; }
}
.
I just need to solve that problem of getting the data correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Steve Py 在他的评论中所说的 ## Order->Department 和 Order->Store 之间的关系不是一对一,而是多对一。 (多个订单可以指同一个部门) ##
根据 https://stackoverflow.com/a/41881299/18399373
您可以使用 virtual 关键字以惰性方式加载相关数据,
实体框架支持三种方式加载相关数据 - 急切加载、延迟加载和显式加载
as Steve Py said in his comment ## The relationship between Order->Department and Order->Store is not One-to-One, it is Many-to-One. (Many orders could refer to the same Department) ##
accroding to https://stackoverflow.com/a/41881299/18399373
you can use virtual keyword to load related data in lazy way
Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading