[英]Bind a related entities to a data grid
I am using Entity Framework 6.1. 我正在使用Entity Framework 6.1。 In my model, two entities Country and Districts . 在我的模型中,有两个实体Country和Districts 。 as: 如:
public class Country
{
public Country()
{
Districts = new HashSet<District>();
}
public int CountryId { get; set; }
public string CountryArabicName { get; set; }
public string CountryEnglishName { get; set; }
public string NationalityArabicName { get; set; }
public string NationalityEnglishName { get; set; }
public virtual ICollection<District> Districts { get; private set; }
}
public class District
{
public int DistrictId { get; set; }
public int CountryId { get; set; }
public string DistrictArabicName { get; set; }
public string DistrictEnglishName { get; set; }
public Country Country { get; set; }
}
So, Country has many Provinces. 因此,乡村有很多省。
In my ViewModel: 在我的ViewModel中:
private IEnumerable<District> _districts;
public IEnumerable<District> Districts
{
get { return _districts; }
set { SetProperty(ref _districts, value); }
}
Then, fill it: 然后,填写:
Districts = (from district in cmsDbContext.Districts
orderby district.DistrictId
select district).ToList();
In the View: 在视图中:
<UserControl.DataContext>
<vm:DistrictVm/>
</UserControl.DataContext>
<DataGrid Height="308" AutoGenerateColumns="False" ItemsSource="{Binding Districts}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DistrictArabicName}" Header="District" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=Country.CountryArabicName}" Header="Country" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
Now, DistrictArabicName is shown correctly. 现在,可以正确显示DistrictArabicName 。 But nothing for Country.CountryArabicName 但是对于Country.CountryArabicName则没有任何意义
What shall I do to solve this?. 我应该怎么做才能解决这个问题? I want to retrieve Country.CountryArabicName 我想检索Country.CountryArabicName
Thanks to Kylo Ren. 多亏Kylo Ren。
What I did to solve the problem: - Remove virtual to disable lazy loading. 解决该问题的方法:-删除虚拟以禁用延迟加载。 The data is too small. 数据太小。 less than 20 records. 少于20条记录。 - Delete all migrations and the database. -删除所有迁移和数据库。 - Re-enable migration again, and update the database. -再次重新启用迁移,并更新数据库。
Now, my example is working. 现在,我的示例正在运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.