Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
'System.Data.Entity.DbSet' does not contain a definition
Message
From
29/06/2014 08:51:20
 
 
To
27/06/2014 16:35:50
General information
Forum:
ASP.NET
Category:
Entity Framework
Environment versions
Environment:
C# 4.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01602673
Message ID:
01602825
Views:
82
>>>I gave you a bad suggestion. The Context property in your GenericRepository is the base DbContext class, which doesn't have DeliveryHistories. DBSet would probably be better to use. If you want to use the context, you will either need to change the type on the property or cast it to your Data_Context class.
>>
>>OK, so just:
>>
>>
return DBSet.Where(dh => dh.RunSheetDetail.RunSheetHeader.rnh_rtefk == id);
>>
>>compiles successfully, thanks.
>>
>>However, I get an error in my DeliveryHistoriesController where I try to call this new method:
>>
>>
        public ActionResult Get(Guid id)
>>        {
>>            DeliveryHistoryViewModel vm = new DeliveryHistoryViewModel();
>>
>>            vm.DeliveryHistory = this._unit.DeliveryHistories.GetByRouteId(id); // error is here
>>
>>            if (vm.DeliveryHistory != null)
>>            {
>>                return View("DeliveryHistory", vm);
>>            }
>>
>>            return View("NotFound");
>>        }
>>
>>The error is:
>>
>>'IBCPackTrack.Data.IRepository< IBCPackTrack.Models.DeliveryHistory>' does not contain a definition for 'GetByRouteId' and no extension method 'GetByRouteId' accepting a first argument of type 'IBCPackTrack.Data.IRepository< IBCPackTrack.Models.DeliveryHistory>' could be found (are you missing a using directive or an assembly reference?)
>>
>>I guess this has something to do with my ApplicationUnit definition:
>>
>>
using System;
>>using IBCPackTrack.Models;
>>
>>namespace IBCPackTrack.Data
>>{
>>    public class ApplicationUnit : IDisposable
>>    {
>>        private DataContext _context = new DataContext();
>>        
>>        private IRepository<AirWayBill> _airwaybills = null;
>>        private IRepository<Customer> _customers = null;
>>        private IRepository<DeliveryHistory> _deliveryhistories = null;
>>        private IRepository<DispositionCode> _dispositioncodes = null;
>>        private IRepository<Route> _routes = null;
>>        private IRepository<RunSheetDetail> _runsheetdetails = null;
>>        private IRepository<RunSheetHeader> _runsheetheaders = null;
>>        private IRepository<ScanCode> _scancodes = null;
>>
>>// code removed for this posting
>>
>>        public IRepository<DeliveryHistory> DeliveryHistories
>>        {
>>            get
>>            {
>>                if (this._deliveryhistories == null)
>>                {
>>                    this._deliveryhistories = new GenericRepository<DeliveryHistory>(this._context);
>>                }
>>                return this._deliveryhistories;
>>            }
>>        }
>>
>>    }
>>}
>>
>>My IRepository is defined like this:
>>
>>
using System;
>>using System.Linq;
>>
>>namespace IBCPackTrack.Data
>>{
>>    public interface IRepository<T> where T : class
>>    {
>>        IQueryable<T> GetAll();
>>        T GetById(Guid id);
>>        void Add(T entity);
>>        void Update(T entity);
>>        void Delete(T entity);
>>        void Delete(Guid id);
>>        void Detach(T entity);
>>    }
>>}
>>
>>I guess I need to do some special kind of repository for my DeliveryHistories, is that right?
>
>Your ApplicationUnit class has _deliveryhistories as the generic IRepository, so it can only use the functions or properties in that interface. The simplest solution is to change the type of that property to your concrete DeliveryHistoryRepository class. If you want to stick with interfaces though you can do the following:
>1) Create an IRouteBasedRepository:
>using System;
>using System.Linq;
>
>namespace IBCPackTrack.Data
>{
>    public interface IRouteBasedRepository< T> : IRepository< T> where T : class
>    {
>        IQueryable< T> GetByRouteId(Guid id);
>    }
>}
>
>2) Add IRouteBasedRepository< DeliveryHistory> as an interface to your DeliveryHistoryRepository class.
>3) Change _deliveryhistories and DeliveryHistories from IRepository< DeliveryHistory> to IRouteBasedRepository< DeliveryHistory>.
>4) Change the get method for DeliveryHistories to create your DeliveryHistoryRepository. The code you posted is creating a generic one that doesn't have your method, and wouldn't implement the new interface.


OK, let's try the simplest solution and once I understand that I'll try the interface route which I guess is probably the better way to do this. I know I'm still missing some fundamentals here in C# and Interfaces so I appreciate your hand holding to walk me through this.

If I understand your suggestion for the simplest solution I should change this:
    public class ApplicationUnit : IDisposable
    {
        private DataContext _context = new DataContext();
        
        private IRepository<DeliveryHistory> _deliveryhistories = null;
to this:
    public class ApplicationUnit : IDisposable
    {
        private DataContext _context = new DataContext();
        
        private DeliveryHistoryRepository _deliveryhistories = null;
Is that right?

If I do that I get a message in my DeliveryHistoriesController:
"Cannot implicitly convert type 'System.Linq.IQueryable<IBCPackTrack.Models.DeliveryHistory>' to 'IBCPackTrack.Models.DeliveryHistory'. An explicit conversion exists (are you missing a cast?)"
This is the code:
        [ActionName("Edit")]
        public ActionResult Get(Guid id)
        {
            DeliveryHistoryViewModel vm = new DeliveryHistoryViewModel();

            vm.DeliveryHistory = this._unit.DeliveryHistories.GetByRouteId(id);

            if (vm.DeliveryHistory != null)
            {
                return View("DeliveryHistory", vm);
            }

            return View("NotFound");
        }
Which I can get around using:
vm.DeliveryHistory = (DeliveryHistory)this._unit.DeliveryHistories.GetByRouteId(id);
Am I doing this correctly?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Reply
Map
View

Click here to load this message in the networking platform