[英]Trying to retrieve embedded/related classes using C# entity framework
Suppose I have a data structure like this - where a Person object may have many addresses, and an address may have manny address lines.假设我有一个这样的数据结构 - 其中一个 Person 对象可能有很多地址,而一个地址可能有很多地址行。
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Address> Addresses { get; set; }
}
public class Address
{
public int Id { get; set; }
public List<AddressLine> AddressLines { get; set; }
public string PostCode { get; set; }
}
public class AddressLine
{
public int Id { get; set; }
public string AddressLine
}
I have then used Entity framework to allow this data structure to be stored into my database thus:然后我使用实体框架来允许将此数据结构存储到我的数据库中,因此:
public class DatabaseContext : DbContext
{
public DatabaseContext() : base("name=DatabaseConnection") { }
public DatabaseContext(string connectionString) : base(connectionString) { }
public DbSet<Person> People { get; set; }
}
Finally, I have a class which interacts with this database:最后,我有一个与这个数据库交互的类:
public class ProcessPeople
{
private DatabaseContext localDatabase;
ProcessPeople(DatabaseContext db)
{
localDatabase = db;
}
public DoProcessingOfPerson(int idOfPerson)
{
Person person =
localDatabase.People.Where(p => p.Id.Equals(idOfPerson)).FirstOrDefault();
Console.WriteLine("First : " + person.FirstName);
Console.WriteLine("Last : " + person.LastName);
// Print address
if(person.Addresses != null && person.Addresses.Count > 0)
{
if(person.Addresses[0].AddressLines != null && person.Addresses[0].AddressLines.Count > 0)
{
Console.WriteLine("Address: " + person.Addresses[0].AddressLines[0].AddressLine);
}
}
else
{
Console.WriteLine("No address available");
}
}
}
The problem is that after the person is retrieved using the code:问题是在使用代码检索到人之后:
Person person =
localDatabase.People.Where(p => p.Id.Equals(idOfPerson)).FirstOrDefault();
The Addresses array is always null - even though there is definitely related Address and AddressLine data in there. Addresses 数组始终为空 - 即使其中肯定有相关的 Address 和 AddressLine 数据。
My question:我的问题:
How does one do a retrieval from the Entity framework database such that all the data in the embedded classes inside the Person class are also retrieved?如何从实体框架数据库中进行检索,以便同时检索到 Person 类中嵌入类中的所有数据?
I am wanting to pull back a person, and then be able to navigate through the Addresses and AddressLines arrays - but these classes are not being retrieved from the database [even though they exist in the actual database that has been generated by entity framework]我想拉回一个人,然后能够浏览 Addresses 和 AddressLines 数组 - 但这些类没有从数据库中检索[即使它们存在于实体框架生成的实际数据库中]
Note that for brevity, i have not included the data input code - I am just looking for what i need to do to this line:请注意,为简洁起见,我没有包含数据输入代码 - 我只是在寻找我需要对这一行做些什么:
Person person =
localDatabase.People.Where(p => p.Id.Equals(idOfPerson)).FirstOrDefault();
In order to get all objects and sub-objects populated in the variable "person", assuming the related data exists.为了获取填充在变量“person”中的所有对象和子对象,假设相关数据存在。
thanks heaps for any assistance感谢您提供任何帮助
David.大卫。
You should use Include as following:您应该使用Include如下:
Person person = localDatabase.People
.Include(p=> p.Addresses)
.ThenInclude(add => add.AddressLine)
.Where(p => p.Id.Equals(idOfPerson)).FirstOrDefault();
For more detail: Loading Related Entities有关更多详细信息: 加载相关实体
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.