MVVM/WPF 应用 RadGridView 绑定到 EntityFrameworkDataSource

MVVM/WPF app RadGridView binding to EntityFrameworkDataSource

我正在尝试将 RadGridView 绑定到 EntityFrameworkDataSource,这样我就可以避免自己编写 paging/filtering,但我无法让它工作。我检查了 Telerik 的示例,但它似乎也不起作用。使用带 EF 6 的 WPF

我的背景:

public class TIMSContext : DbContext
{
    public TIMSContext()
        : base("name=TIMSContext") { }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //map models here...
    }

    public IQueryable<T> Query<T>()
    {
        return this.Set(typeof(T)).AsQueryable() as IQueryable<T>;
    }

}

我的XAML:

     <telerik:RadGridView x:Name="RadGridView1"
                             Grid.Row="0"
                             AlternateRowBackground="CornflowerBlue"
                             AlternationCount="2"
                             AutoGenerateColumns="True"
                             CanUserDeleteRows="False"
                             CanUserFreezeColumns="False"
                             CanUserInsertRows="False"
                             GroupRenderMode="Flat"
                             IsReadOnly="True"
                             ItemsSource="{Binding ShipmentData}"
                             RowIndicatorVisibility="Collapsed" />

        <telerik:RadDataPager Grid.Row="1"
                              DisplayMode="FirstLastPreviousNextNumeric"
                              PageSize="25"
                              Source="{Binding ShipmentData}" />

我的视图模型:

public class ShipmentsViewModel :ViewModelBase
{
    private readonly QueryableEntityCollectionView<TIMS.Data.Entities.ASN> shipments;
    private readonly TIMS.Data.TIMSContext ctx;

    public ShipmentsViewModel()
    {
        try
        {
            ctx = new Data.TIMSContext();
            shipments = new QueryableEntityCollectionView<Data.Entities.ASN>(ctx, "ASN");
            //Argument 1: cannot convert from 'TIMS.Data.TIMSContext' to 'System.Data.Objects.ObjectContext'


            //Also tried this which I saw on some forums...
            //shipments = new QueryableEntityCollectionView<Data.Entities.ASN>( ((IObjectContextAdapter)ctx).ObjectContext, "ASN"); 
            //Argument 1: cannot convert from 'System.Data.Entity.Core.Objects.ObjectContext' to 'System.Data.Objects.ObjectContext'

        }
        catch (Exception)
        {
        }
    }

    #region Properties

    public Object ShipmentData
    {
        get
        {
            return shipments;
        }
    }

    #endregion

}

原来有一个特定的 EF 6 程序集....需要参考 Telerik.Windows.Controls.EntityFramework60。并且必须像上面的代码一样将 DbContext 转换为 IObjectContextAdapater。

此外,您必须在每个 DbSet 的上下文中显式定义属性,并在进行函数调用时提供名称。那部分有点烦人...