是否有可能从EF中的Sp延迟加载?

时间:2016-07-18 15:12:39

标签: c# entity-framework lazy-loading

我有一个学校班级,里面有一个房间列表

public class School
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual IList<Room> Rooms { get; set; }
}

public class Room
{
    public int Id { get; set; }
    public int SchoolId { get; set; }
    public string Name { get; set; }
}

现在我想用存储过程获取他们房间的学校列表。

我知道&#39; Include&#39;在代码优先模式下运行。

它可能在NHibernate和MyBatis.Net Orms。

是否可以在EF?

1 个答案:

答案 0 :(得分:0)

您可以将SP与DbFunction一起使用以获得惰性结果:

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Grid>
                        <!-- 
                        Stole content presentation from here: 
                        http://stackoverflow.com/a/14266323/424129
                        -->
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter 
                                Grid.Column="1"
                                ItemsPanel="{TemplateBinding ItemsPanel}"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                />
                            <DataGridDetailsPresenter 
                                Grid.Column="1"
                                Grid.Row="1"
                                SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                Visibility="{TemplateBinding DetailsVisibility}" 
                                />
                            <DataGridRowHeader 
                                Grid.RowSpan="2"
                                SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
                                Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 
                                />
                        </SelectiveScrollingGrid>
                        <Popup
                            x:Name="RowPopup"
                            IsOpen="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}"
                            >
                            <Border 
                                MinWidth="200" 
                                MinHeight="166" 
                                Background="WhiteSmoke"
                                BorderBrush="Black"
                                BorderThickness="1"
                                Padding="12"
                                >
                                <TextBlock>
                                    <TextBlock Text="Blah blah popup. " />
                                    <TextBlock Text="{Binding IsOdd, StringFormat=IsOdd: {0} }" />
                                </TextBlock>
                            </Border>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <!-- 
                        The RelativeSource=TemplatedParent binding doesn't seem to be working here. 
                        I don't understand why not. Probably something stupid and obvious. 
                        -->
                        <!-- 
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition 
                                    Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}" 
                                    Value="True" />
                                <Condition Binding="{Binding IsOdd}" Value="False" />
                            </MultiDataTrigger.Conditions>
                            <Setter TargetName="RowPopup" Property="IsOpen" Value="True" />
                        </MultiDataTrigger>
                        -->

                        <!-- 
                        So instead, we bind IsOpen on the popup to IsSelected, and then 
                        override that with False if the boolean property is true. Clumsy 
                        but it works. 
                        -->
                        <DataTrigger Binding="{Binding IsOdd}" Value="True">
                            <Setter TargetName="RowPopup" Property="IsOpen" Value="False" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.RowStyle>

现在您可以使用查询并实现IQueryable,您必须调用ToList(),First()等。此命令将发送数据库。

您可以为Room类做同样的事情。

以下是一个例子:

https://codefirstfunctions.codeplex.com/