0

I have 4 layers

  1. Controller Layer

     public class BaseMapController : ApiController
     {
         /// <summary>
         /// Create an object for service layer interface and inject the object usinng constructor injection
         /// </summary>
         /// 
         public BaseMapController()
         {
    
         }
         private IBaseMapServiceInterface _basemapServiceInterface;
    
         public BaseMapController(IBaseMapServiceInterface thebasemapServiceInterface)
         {
             _basemapServiceInterface = thebasemapServiceInterface;
         }
    
         [HttpGet]
         public IEnumerable<MasterTblModel> GetSearch()
         {
    
           return _basemapServiceInterface.GetSearch();
    
         }
     }
    

    }

  2. Service Layer

     public class BaseMapServiceInterfaceImpl : IBaseMapServiceInterface
     {
         /// <summary>
         /// Create an object for data access layer interface and inject the object usinng constructor injection
         /// </summary>
         /// <returns></returns>
    
         private IBaseMapDataInterface _basemapDataInterface;
    
         public BaseMapServiceInterfaceImpl(IBaseMapDataInterface thebasemapDataInterface)
         {
             _basemapDataInterface = thebasemapDataInterface;
         }
         public IEnumerable<MasterTblModel> GetSearch()
         {
             return _basemapDataInterface.GetSearch();
         }
     }
    
  3. DataAccessLayer

     public class BaseMapDataInterfaceImpl : IBaseMapDataInterface
     {
    
         public IEnumerable<MasterTblModel> GetSearch()
         {
             using (KGISApplication_Context Con = new KGISApplication_Context())
             {
    
                 **return (IEnumerable<MasterTblModel>)Con.MASTER_TBL;**
    
    
             }
         }
     }
    
  4. Model class

     public class MasterTblModel
     {
         public string MASTER_TBL_ID { get; set; }
         public string category { get; set; }
         public string Layer_Name { get; set; }
         public string table_alias { get; set; }
     }
    

solution is not have any compile time error, when I run it is showing 'System.Data.Entity.DbSet1[DataAccessLayer.MASTER_TBL]' to type 'System.Collections.Generic.IEnumerable1[KGISModels.MasterTblModel]'.' from the data access layer

I did all the effort to type cast and change the linq query style, but could not able to fix. Any help will be helpful. For DI I have used unity framework and registered the interface to implementation class properly.

Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
Jaya
  • 7
  • 4
  • ASP.NET MVC is a web app framework, not a data access library or ORM. The problem is that you're trying to use Entity Framework, an ORM, in an inappropriate way. A DbSet is neither a table nor a collection of objects. It's a *repository* for a specific entity, that's used to create LINQ queries. To retrieve data you need to execute those queries with `ToListAsync`, `ToArrayAsync` etc. In the rare case where you need to load an entire table into memory, you can use `ToListAsync` directly – Panagiotis Kanavos Mar 16 '21 at 07:04
  • 1
    An *entity* called `Master_TBL` is extremely unusual though. And using `con` for a DbContext suggests there's a misunderstanding on what EF is and how it works. A DbContext is neither a database connection nor a model of the database. It *uses* connections only when it has to load data or save changes. A DbContext is a Unit-of-Work managing the *entities* used in a specific scenario. You can have multiple DbContexts in the same application, that make cover the same tables in different shapes, to cover different scenarios/use cases. Only small applications can get away with 1 DbContext – Panagiotis Kanavos Mar 16 '21 at 07:09
  • BTW you didn't post the relevant code. Since this has nothing to do with MVC, the controller and service class aren't involved. What matters is `KGISApplication_Context` and the class exposed by `MASTER_TBL`. Is it `MasterTblModel`, another unexpected name? Or is it a *different* class that you want mapped to `MasterTblModel`? `change the linq query style` you didn't post *any* query at all – Panagiotis Kanavos Mar 16 '21 at 07:11
  • If you want to map a different class to `MasterTblModel` you need to use `Select`. And use proper names - `MasterTblModel` sounds like a Doctor Who episode. The entity names should actually reflect their use. Let's say you have Sales or Inventory and orders. That's two scenarios where the Order or Customer **entities** will have different shapes. Let's assume sales, the DbContext should be eg `SalesContext` and the entities could be `Order` and `OrderDetails` exposed as `Orders` and `OrderDetails`. To map Orders to a ViewModel you need `context.Orders.Select(ord=>new OrderModel(....))` – Panagiotis Kanavos Mar 16 '21 at 07:16
  • The class and interface names, `BaseMapDataInterfaceImpl`, `IBaseMapDataInterface` and a `GetSearch` that returns everything is another strong indicator that something's wrong. EF is already abstract, it doesn't need an abstraction on top of it unless it's to *specialize* its use. Searching is the job of the database. Loading everything in memory and trying to search it without the help of a server's indexes, dozens of CPU cores and fast multi-SSD arrays is only going to slow things down – Panagiotis Kanavos Mar 16 '21 at 07:19
  • Relevant: [What is the difference between a model and an entity?](https://stackoverflow.com/a/8744288/2791540) – John Wu Mar 16 '21 at 07:27
  • Thank you sir, I changed the query like this using (KGISApplication_Context Con = new KGISApplication_Context()) { var result = Con.MASTER_TBL.Select(x => new MasterTblModel() { category = x.category, Layer_Name=x.Layer_Name, .... }).ToList(); return result; and it return the values. Even I have installed the swagger and the api is working super fine. – Jaya Mar 16 '21 at 10:16

0 Answers0