I use Entity Framework for data access. There is a context class:

 public class Context : DbContext { public string cs; public Context(string connection_string) { Database.Connection.ConnectionString = connection_string; cs = connection_string; } public DbSet<Customers> cust { get; set; } public DbSet<Orders> ord { get; set; } public DbSet<Cars_In_Stock_Table> cars { get; set; } } 

Trying to get data:

 public IEnumerable<Cars_In_Stock_Table> GetAllCars() { return context.cars; } 

Visual Studio regarding context.cars gives:

Implicit conversion ... DbSet <...> to ... IEnumerable <...> impossible

Saw examples where this approach works. You can use ToList() , but I would like to understand the reason. Could this be related to the Entity Framework version?

  • where context.cars.Load ()? and why not then context.cars.Local? - Konst
  • Why do you need it? - AN90
  • Give the full error code and EF version. at 6.0 the studio on your code is silent. - PashaPash

2 answers 2

The method should return an IEnumerable<Cars_In_Stock_Table> , and you DbSet DbSet ... Accordingly, implicitly, you cannot convert one type to another. You can try this:

 public IEnumerable<Cars_In_Stock_Table> GetAllCars() { return context.cars.AsEnumerable(); // или ToList(); } 

Or so:

 public IQueryable<Cars_In_Stock_Table> GetAllCars() { return context.cars; } 

Also, regarding the naming of class fields, I advise you to read this article: C # code writing conventions .

    This is not related to the version of the framework, it is only due to the fact that DbSet is not IEnumerable, although its base class DbQuery uses this interface as well. But if you use the data set received from their database as a List (List), then you need to convert DbSet to List using the ToList method.