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:09:58
 
 
À
27/06/2014 13:08:40
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:
01602720
Vues:
54
>>>Hi,
>>>
>>>I am working on an ASP.NET MVC app using Web API.
>>>
>>>I am trying to create the ability to pull back a subset of data to display on a page.
>>>
>>>So I have my repository defined like this:
>>>
>>>
using IBCPackTrack.Models;
>>>using System;
>>>using System.Data.Entity;
>>>using System.Linq;
>>>
>>>namespace IBCPackTrack.Data
>>>{
>>>    public class DeliveryHistoryRepository : GenericRepository<DeliveryHistory>
>>>    {
>>>        public DeliveryHistoryRepository(DbContext context)
>>>            : base(context)
>>>        {
>>>
>>>        }
>>>
>>>        public IQueryable<DeliveryHistory> GetByRouteId(Guid id)
>>>        {
>>>            return DbSet.Select(dh => new DeliveryHistory
>>>            {
>>>                del_pk = dh.del_pk,
>>>                del_time = dh.del_time,
>>>            });
>>>
>>>        }
>>>    }
>>>}
>>>
>>>This is giving me a syntax error under the Select in "DbSet.Select":
>>>
>>>'System.Data.Entity.DbSet' does not contain a definition for 'Select'
>>>
>>>I'm not sure why as this code works fine in another application that I am using as an example.
>>>
>>>Basically what I want to do is accept a key and use that key to produce a subset of data from DeliveryHistory like this SQL would produce:
>>>
>>>SELECT DeliveryHistory.*
>>>FROM DeliveryHistory
>>>INNER JOIN RunSheetDetails on del_rndfk = rnd_pk
>>>INNER JOIN RunsheetHeaders on rnd_rnhfk = rnh_pk
>>>WHERE rnh_rtefk = @RouteId
>>>
>>>Any help appreciated!
>>
>>DbSet is a type which corresponds to your tables. Your repository should have a property of type DbSet< DeliveryHistory> that you would use as your starting point. That is, unless your GenericRepository class defines DbSet as a property.
>>
>>As for your query, there are a couple of things you will want to change:
>>1) DeliveryHistory is an entity object, and entity framework doesn't like you using those objects for selecting a subset of the data. You'll need to define a new class to do the subset of data.
>>2) Your query isn't filtering the data at all.
>>
>>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,
>> });
>>}
>>
>
>One of these days this will all make sense :)
>
>When I try your suggested code I get this error: "the name 'context' does not exist in the current context"
>
>My Data Context is defined like this:
>
>
using System;
>using System.Configuration;
>using System.Data;
>using System.Data.Entity;
>using System.Linq;
>
>using IBCPackTrack.Models;
>using IBCPackTrack.Data.Configuration;
>
>namespace IBCPackTrack.Data
>{
>    class DataContext : DbContext
>    {
>        public DbSet<Route> Routes { get; set; }
>        public DbSet<AirWayBill> AirWayBills { get; set; }
>        public DbSet<Customer> Customers { get; set; }
>        public DbSet<DeliveryHistory> DeliveryHistories { get; set; }
>        public DbSet<DispositionCode> DispositionCodes { get; set;}
>        public DbSet<RunSheetHeader> RunSheetHeaders { get; set; }
>        public DbSet<RunSheetDetail> RunSheetDetails { get; set; }
>        public DbSet<ScanCode> ScanCodes { get; set; }
>
>        public static string ConnectionStringName
>        {
>            get
>            {
>                if (ConfigurationManager.AppSettings["ConnectionStringName"] != null)
>                {
>                    return ConfigurationManager.AppSettings["ConnectionStringName"].ToString();
>                }
>
>                return "DefaultConnection";
>            }
>
>        }
>
>        public DataContext()
>            : base(nameOrConnectionString: DataContext.ConnectionStringName) 
>        {
>            // necessary to stop this error:
>            // Self referencing loop detected for property 'route' with type 'System.Data.Entity.DynamicProxies.Route_
>            this.Configuration.LazyLoadingEnabled = false;
>        }
>
>        protected override void OnModelCreating(DbModelBuilder modelBuilder)
>        {
>            modelBuilder.Configurations.Add(new AirWayBillConfiguration());
>            modelBuilder.Configurations.Add(new CustomerConfiguration());
>            modelBuilder.Configurations.Add(new DeliveryHistoryConfiguration());
>            modelBuilder.Configurations.Add(new DispositionCodeConfiguration());
>            modelBuilder.Configurations.Add(new RouteConfiguration());
>            modelBuilder.Configurations.Add(new RunSheetDetailConfiguration());
>            modelBuilder.Configurations.Add(new RunSheetHeaderConfiguration());
>            modelBuilder.Configurations.Add(new ScanCodeConfiguration());
>            //base.OnModelCreating(modelBuilder);
>        }
>    }
>}
>
>What have I done wrong?

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

Click here to load this message in the networking platform