简体   繁体   English

wpf datagrid xaml和实体框架

[英]wpf datagrid xaml and entity framework

I want to display in a Column navigation property like city_Name I wrote this code but Nothing happens 我想显示在诸如city_Name之类的列导航属性中,我编写了此代码,但没有任何反应

      <DataGrid Margin="210,15"  Grid.Row="1" CanUserAddRows="False" AutoGenerateColumns="False" Name="dg_Super" RowHeight="20" >
        <DataGrid.Columns>
            <DataGridTextColumn Width="*" Header="Super Name"   Binding="{Binding Path=SupermarketName, Mode=OneTime}"></DataGridTextColumn>
            <DataGridTextColumn Width="*" Header="City"  Binding="{Binding Path=City1.CityName, Mode=OneTime}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

and the the c# code 和C#代码

            var super = mh.Supermarkets;
            dg_Super.ItemsSource = super.ToList();

City1 is anavigation property from ef model. City1是ef模型的导航属性。 Please what is wrong here? 请问这里有什么问题?

In your query - perhaps add the Include method: 在您的查询中-也许添加Include方法:

var super = mh.Supermarkets.Include("City1")

Because EF doens't know it has to get data from the "City" table. 因为EF不知道它必须从“城市”表中获取数据。 If the query uses a field from the City table, you don't have to include Include("City") - EF can then detect that you need data from the table, thus it automaticly loads the City table 如果查询使用“城市”表中的字段,则不必包括Include("City") -EF可以检测到您需要该表中的数据,从而自动加载“城市”表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM