MVVM Binding RadGridView to EntityFrameworkDataSource

4 posts, 0 answers
  1. Samuel
    Samuel avatar
    8 posts
    Member since:
    Jul 2014

    Posted 21 May 2015 Link to this post

    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
    }

  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 22 May 2015 Link to this post

    Hi,

    There should not be problems populating RadGridView with data using EF6. You can check the RadEntityFrameworkDataSource examples in  WPF Demos for a reference.

    Please note that in order to get RadEntityFrameworkDataSource working with EF6, you should reference Telerik.Windows.Controls.EntityFramework60 assembly.

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Samuel
    Samuel avatar
    8 posts
    Member since:
    Jul 2014

    Posted 22 May 2015 in reply to Dimitrina Link to this post

    The demo doesn't work correctly.  It's missing the cast of the context to the IObjectContextAdapter.  However, I've gotten that piece resolved.  However, it seems the only way to do this is to pass in a sting ("ASN" in the code above) with the property name of the DbSet in the context.  But as you can see from my implementation above, I don't have explicitly defined properties.  Is there a way to do this without defining them?

     

     

  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 25 May 2015 Link to this post

    Hello,

    As it turns out I am not sure if this is the only way to resolve it.  Would it be possible for you to illustrate the specific case in a demo project with your exact setup and send it to us via a new support ticket? That way we may debug the solution with the source code directly and advise further.

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top