EF Core One To Many包括

时间:2018-05-29 11:41:50

标签: c# linq asp.net-core-mvc entity-framework-core

我使用EF-CORE在ASP.NET核心MVC网站上工作,在我的数据库中,我有一个" Doc"表格和签名"签名"表,一个Doc可以有多个签名,一个签名只能在Doc上,这里是我的Code First Entity Model:

文件:

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; }
}

签名: 公共类签名     {

    //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();

签名变空

如何按DocType订购此列表?

3 个答案:

答案 0 :(得分:1)

订单上实际上有一个错误,但它不是throw:DocType是另一个表,并没有实现IComparable所以我只是改变了这样的请求:

List<Doc> docs = _applicationDBContext.Doc
                                .Include(d => d.Signatures)
                                    .ThenInclude(s=>s.Employee)
                                .Include(d => d.DocType)
                                .OrderBy(d => d.DocType.Name)
                                .ToList();

它完美无缺。

答案 1 :(得分:0)

在你的OnModelCreating of DbContext中,你有一个Doc和Signature的关系吗?像这样的东西:

modelBuilder.Entity<Signature>().HasOne(m => m.Doc).WithMany(m => m.Signature).HasForeignKey(m => m.DocID);

答案 2 :(得分:0)

查看ef core page on relationships

你应该可以简化为这样的事情。

请记住许多事情按惯例工作,因此命名很重要(例如,使用实体框架自动获取实体密钥的ID)

如果您在'子'对象上有向后导航属性,则需要外键属性和id属性或使用Fluent API。

public class Doc
{
    public int Id { get; set; }

    public DocType DocType { get; set; }

    [Required]
    public int Version { get; set; }

    [Required]
    public Byte[] Data { get; set; }

    public List<Signature> Signatures { get; set; }
}

public class Signature {

    public int Id { get; set; }

    //backwards navigation
    public int DocForeignKey {get;set;}
    [ForeignKey("DocForeignKey")]
    public Doc Doc { get; set; }

    public 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; }
}
相关问题