I am using EF 6 to connect to a database in a WPF app using MVVM pattern. I have a derived context like so:
public class TIMSContext : DbContext { public TIMSContext() : base("name=TIMSContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //map models... } public IQueryable<T> Query<T>() { return this.Set(typeof(T)).AsQueryable() as IQueryable<T>; } }
I want to use the EntityFrameworkDataSource to provide items to a RadGridView so that I don't have to write the paging/filtering code myself. I have looked at the example, but I cannot get it to work.
My 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}"/>
Here is my Viewmodel:
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}