Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
'System.Data.Entity.DbSet' does not contain a definition
Message
De
27/06/2014 14:48:03
 
 
À
27/06/2014 14:43:24
Information générale
Forum:
ASP.NET
Catégorie:
Entity Framework
Versions des environnements
Environment:
C# 4.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01602673
Message ID:
01602729
Vues:
66
>>>>>>Your query should look something like this:
>>>>>>
>>>>>>public IQueryable< DeliveryHistory> GetByRouteId(Guid id)
>>>>>>{
>>>>>>return context.DeliveryHistories // The context property that controls access to our data
>>>>>>.Where(dh => dh.RunSheetDetail.RunSheetHeader.rnh_rtefk == id) // Filter the data by the routes
>>>>>>.Select(dh => new DeliveryHistorySimple() // Select a subset of the data. If you want all of the DeliveryHistory entity, get rid of the select function
>>>>>>{
>>>>>>                del_pk = dh.del_pk,
>>>>>>                del_time = dh.del_time,
>>>>>> });
>>>>>>}
>>>>>>
>>>
>>>>context in my example should be an instance of your DataContext class. Your DeliveryHistoryRepository class takes it in as a parameter in the constructor, but without knowing the base class, I don't know what property it saves it under.
>>>
>>>This is the GenericRepository class, which is the base class for DeliveryHistoryRepository:
>>>
>>>
using System;
>>>using System.Data;
>>>using System.Data.Entity;
>>>using System.Data.Entity.Infrastructure;
>>>using System.Linq;
>>>
>>>namespace IBCPackTrack.Data
>>>{
>>>    public class GenericRepository<T> : IRepository<T> where T : class
>>>    {
>>>        protected DbSet<T> DBSet { get; set; }
>>>        protected DbContext Context { get; set; }
>>>
>>>        public GenericRepository(DbContext context)
>>>        {
>>>            if (context == null)
>>>            {
>>>                throw new ArgumentException("An instance of DbContext is " +
>>>                    "required to use this repository.", "context");
>>>            }
>>>
>>>            this.Context = context;
>>>            this.DBSet = this.Context.Set<T>();
>>>        }
>>>
>>>        public virtual IQueryable<T> GetAll()
>>>        {
>>>            return this.DBSet;
>>>        }
>>>
>>>        public virtual T GetById(Guid id)
>>>        {
>>>            return this.DBSet.Find(id);
>>>        }
>>>
>>>        public virtual void Add(T entity)
>>>        {
>>>            DbEntityEntry entry = this.Context.Entry(entity);
>>>            if (entry.State != EntityState.Detached)
>>>            {
>>>                entry.State = EntityState.Added;
>>>            }
>>>            else
>>>            {
>>>                this.DBSet.Add(entity);
>>>            }
>>>        }
>>>
>>>        public virtual void Update(T entity)
>>>        {
>>>            DbEntityEntry entry = this.Context.Entry(entity);
>>>
>>>            if (entry.State == EntityState.Detached)
>>>            {
>>>                this.DBSet.Attach(entity);
>>>            }
>>>
>>>            entry.State = EntityState.Modified;
>>>        }
>>>
>>>        public virtual void Detach(T entity)
>>>        {
>>>            DbEntityEntry entry = this.Context.Entry(entity);
>>>
>>>            entry.State = EntityState.Detached;
>>>        }
>>>
>>>        public virtual void Delete(T entity)
>>>        {
>>>            DbEntityEntry entry = this.Context.Entry(entity);
>>>
>>>            if (entry.State != EntityState.Deleted)
>>>            {
>>>                entry.State = EntityState.Deleted;
>>>            }
>>>            else
>>>            {
>>>                this.DBSet.Attach(entity);
>>>                this.DBSet.Remove(entity);
>>>            }
>>>        }
>>>
>>>        public virtual void Delete(Guid id)
>>>        {
>>>            var entity = this.GetById(id);
>>>
>>>            if (entity != null)
>>>            {
>>>                this.Delete(entity);
>>>            }
>>>        }
>>>    }
>>>}
>>>
>>>
>>>Does that show you what it should be?
>>
>>Yep. In my example you can change context.DeliveryHistories to either Context.DeliveryHistories or just DBSet (note the B is capitalized, which it wasn't in your original code). Both Context.DeliveryHistories and DBSet point to the same set of records.
>
>Bummer, that's what I had first tried and it gives me this error:
>
>'System.Data.Entity.DbContext' does not contain a definition for 'DeliveryHistories' and no extension method 'DeliveryHistories' accepting a first argument of type 'System.Data.Entity.DbContext' could be found (are you missing a using directive or an assembly reference?)
>
>Sorry for making you spend so much time on me :(

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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform