【发布时间】:2018-11-08 01:25:09
【问题描述】:
我在我的数据库中使用 EF-CORE 开发 ASP.NET Core MVC 网站。
我有一个Doc 表和一个Signature 表:
- 一个
Doc可以有多个Signatures - 一个
Signature只能在Doc上拥有。
这是我的 Code First 实体模型:
文档
public class Doc
{
[Key]
public int DocID { get; set; }
[Required]
public int DocTypeID { get; set; }
[ForeignKey("DocTypeID")]
public virtual DocType DocType { get; set; }
[Required]
public int Version { get; set; }
[Required]
public Byte[] Data { get; set; }
[ForeignKey("DocID")]
public List<Signature> Signatures { get; set; }
}
签名
public class Signature
{
//FK ITPolicyVersion
[Key]
public int DocID { get; set; }
[ForeignKey("DocID")]
public virtual Doc Doc { get; set; }
//FK EmployeeID
[Key]
public int EmployeeID { get; set; }
[ForeignKey("EmployeeID")]
public virtual Employee Employee { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Signature Date")]
public DateTime? SignatureDate { get; set; }
public String Value { get; set; }
}
但是当我使用这个请求时:
_applicationDBContext.Doc
.Include(d => d.DocType)
.Include(d => d.Signatures)
.OrderBy(d => d.DocType)
.ToList();
d.Signatures 始终为空,我不知道为什么。
这是我在 SQL 中尝试做的事情:
SELECT * FROM Doc d
JOIN DocType dt ON dt.DocTypeID = d.DocTypeID
JOIN Signature s ON s.DocID = d.DocID
JOIN Employee e ON e.EmployeeID = s.EmployeeID
这在 SQL 中运行良好,但不适用于 LINQ
编辑
这项工作:
List<Doc> docs = _applicationDBContext.Doc
.Include(d => d.Signatures)
.ThenInclude(s => s.Employee)
.Include(d => d.DocType)
.ToList();
但不是这个:
List<Doc> docs = _applicationDBContext.Doc
.Include(d => d.Signatures)
.ThenInclude(s => s.Employee)
.Include(d => d.DocType)
.OrderBy(d => d.DocType)
.ToList();
Signature 变空了
如何通过DocType订购此列表?
【问题讨论】:
标签: c# linq asp.net-core-mvc entity-framework-core