[英]Accessing child object from parent object in EF Core
I've searched the whole net, but still I can't get this to work.我已经搜索了整个网络,但仍然无法使其正常工作。
What I need is a simple list that looks like this:我需要的是一个简单的列表,如下所示:
I have one table of the Parent objects and one of the Child objects.我有一张父对象表和一个子对象表。 In the table of Child objects, there is a foreign key ParentId.在 Child 对象表中,有一个外键 ParentId。
Now, I use strongly typed ViewModel and Intellisense even suggests the Child properties through a Parent.Child.Property, but then when I run the program I get a "NullReferenceException: Object reference not set to an instance of an object.".现在,我使用强类型的 ViewModel,Intellisense 甚至通过 Parent.Child.Property 建议 Child 属性,但是当我运行程序时,我得到一个“NullReferenceException:对象引用未设置为对象的实例。”。
My Parent class looks like this:我的父类看起来像这样:
public class EmployeeArea
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public int? StudioCode { get; set; }
public string AreaName { get; set; }
public List<EmployeeName> EmployeeNames { get; set; }
}
}
And my Child class looks like this:我的 Child 类看起来像这样:
public class EmployeeName
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Name { get; set; }
public int AreaId { get; set; }
}
}
Do I need to do something in the DbContext ModelBuilder?我需要在 DbContext ModelBuilder 中做些什么吗? I welcome any help.我欢迎任何帮助。 Thank you!谢谢!
Well, I can't see your query but I'd suggest the following.好吧,我看不到您的查询,但我建议如下。 First, the foreign key should be :首先,外键应该是:
public int EmployeeAreaId {get; set;}
then you have two options to get data from database.那么你有两种选择从数据库中获取数据。 If you are using EF, then:如果您使用的是 EF,则:
db.EmployeeArea.Include(e => e.EmployeeName).ToList();
which will return a list of employeeArea including the child list.这将返回一个员工区域列表,包括子列表。
Using Linq will be like :使用 Linq 将类似于:
var employeeArea = (from e in db.EmployeeArea
select new EmployeeArea
{
Id = e.Id
// continue populating the model properties
EmployeeNames = db.EmployeeName.Where(e =>
e.EmployeeAreaId == e.Id).ToList(),
}).ToList()
Excuse any typo here.请原谅这里的任何错字。 Hope it helps.希望能帮助到你。
Be sure when looping through parents to check if their child list is null .确保在循环访问父母时检查他们的孩子列表是否为null 。
You have to first define the foreign key properly by overriding OnModelCreating or by convention:您必须首先通过覆盖 OnModelCreating 或按照约定正确定义外键:
public int EmployeeAreaId {get; set;}
If you are using strongly typed ViewModels you need to use EFCore Projections( https://entityframeworkcore.com/querying-data-projection ), the code would look something like this(replace with your viewmodel names):如果您使用的是强类型视图模型,则需要使用 EFCore 投影( https://entityframeworkcore.com/querying-data-projection ),代码将如下所示(替换为您的视图模型名称):
db.EmployeeAreas.Select(e => new YourViewModel{
ParentName = e.AreaName,
ChildNames = e.EmployeeNames.Select(en => new List<string>{
EmployeeName = en.Name
}).ToList();
}).ToList();
If this were Json it would look like this:如果这是 Json,它将如下所示:
[
{
ParentName : "ParentName1",
ChildNames ; ["ChildName1","ChildName2"]
},
{
ParentName : "ParentName2",
ChildNames ; ["ChildName3"]
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.