实体框架核心.include()问题 [英] Entity Framework core .Include() issue

查看:72
本文介绍了实体框架核心.include()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾经玩过ef core,并且对include语句有疑问。对于此代码,我得到了2家符合我期望的公司。 GetAllCompanies(HsDbContext db)
{
var c = db.Company;
返回c;
}

此返回

  [
{
id:1,
companyName: new,
admins:null,
employees:null,
courses:null
},
{
id:2,
companyName:测试公司,
admins:null,
employees:null,
courses:null
}
]

您可以看到有2家公司,并且所有相关属性为空,因为我使用过任何包含项,这就是我期望。现在,当我将方法更新为:

  public IEnumerable< Company> GetAllCompanies(HsDbContext db)
{
var c = db.Company
.Include(t => t.Employees)
.Include(t => t.Admins)
.ToList();

返回c;
}

返回的结果是:

  [
{
id:1,
companyName: new,
admins:[
{
id:2,
forename: User,
surname: 1,
companyId:1
}
]
}
]

它仅返回一个公司,并且仅包括管理员。为什么不包括这两家公司及其雇员?

 公共类公司
{
public ind ID {get;组; }
公用字串CompanyName {get;组; }
公共列表< Admin>管理员{组; }
个公共列表< Employee>员工{得到;组; }
公开清单<课程>课程{获得;组; }

公共字符串GetFullName()
{
return CompanyName;
}
}

公共类员工
{
public int Id {get;组; }
公共字符串姓氏{get;组; }
公用字串Surname {get;组; }
public int CompanyId {get;组; }
[ForeignKey( CompanyId)]
上市公司{组; }

公共ICollection< EmployeeCourse>员工课程组; }
}

public class Admin
{
public int Id {get;组; }
公共字符串姓{组; }
公用字串Surname {get;组; }
public int CompanyId {get;组; }
[ForeignKey( CompanyId)]
公开公司Company {get;组; }
}


解决方案

我不确定如果您已看到此问题,但是问题与JSON序列化程序如何处理循环引用有关。完整的详细信息和更多参考的链接可以在上面的链接中找到,我建议您进行深入研究,但总之,将以下内容添加到 startup.cs 即可进行配置序列化程序忽略循环引用:

  services.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});


Been having a play about with ef core and been having an issue with the include statement. For this code I get 2 companies which is what i expected.

public IEnumerable<Company> GetAllCompanies(HsDbContext db)
{
    var c = db.Company;
    return c;
}

This returns

[
    {
        "id":1,
        "companyName":"new",
        "admins":null,
        "employees":null,
        "courses":null
    },
    {
        "id":2,
        "companyName":"Test Company",
        "admins":null,
        "employees":null,
        "courses":null
    }
]

As you can see there are 2 companies and all related properties are null as i havnt used any includes, which is what i expected. Now when I update the method to this:

public IEnumerable<Company> GetAllCompanies(HsDbContext db)
{
    var c = db.Company
        .Include(t => t.Employees)
        .Include(t => t.Admins)
        .ToList();

    return c;
}

this is what it returns:

[
    {
        "id":1,
        "companyName":"new",
        "admins":[
            {
                "id":2,
                "forename":"User",
                "surname":"1",
                "companyId":1
            }
        ]
    }
]

It only returns one company and only includes the admins. Why did it not include the 2 companies and their employees?

public class Company
{
    public int Id { get; set; }
    public string CompanyName { get; set; }
    public List<Admin> Admins { get; set; }
    public List<Employee> Employees { get; set; }
    public List<Course> Courses { get; set; }

    public string GetFullName()
    {
        return CompanyName;
    }
}

public class Employee
{
    public int Id { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public int CompanyId { get; set; }
    [ForeignKey("CompanyId")]
    public Company company { get; set; }

    public ICollection<EmployeeCourse> Employeecourses { get; set; }
}

public class Admin
{
    public int Id { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public int CompanyId { get; set; }
    [ForeignKey("CompanyId")]
    public Company Company { get; set; }
}

解决方案

I'm not sure if you've seen the accepted answer to this question, but the problem is to do with how the JSON Serializer deals with circular references. Full details and links to more references can be found at the above link, and I'd suggest digging into those, but in short, adding the following to startup.cs will configure the serializer to ignore circular references:

services.AddMvc()
    .AddJsonOptions(options => {
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });

这篇关于实体框架核心.include()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆