c# - Correct implementation of DAL with Entity Framework & Repository -


i'm having difficulty , confusion setting mvc/webapi project separate dal project.

i have class library project titled "dashboard.data", generated code-first existing database. class library contains model classes repository. i've yet implement generic repository have far should still work.

repository:

namespace dashboard.data.repository {     public class productrepository : iproductrepository, idisposable     {         private databasecontext _context;         public productrepository()         {             _context = new databasecontext();         }         public productrepository(databasecontext context)         {             this._context = context;         }          public async task<ienumerable<department>> getdepartmentlist()         {             return await _context.departments.tolistasync();         }         protected void dispose(bool disposing)         {             if (disposing)                 if (_context != null)                 {                     _context.dispose();                     _context = null;                 }         }         public void dispose()         {             dispose(true);             gc.suppressfinalize(this);         }     } } 

irepository:

namespace dashboard.data.repository {     public interface iproductrepository: idisposable     {         task<ienumerable<department>> getdepartmentlist();     } } 

now have separate mvc/webapi project, try use repository.

my apicontroller:

namespace dashboard.controllers {     public class productsapicontroller : apicontroller     {         protected iproductrepository repository { get; set; }         public productsapicontroller()         {             this.repository = new productrepository();         }          [route("api/departments")]         public async task<ihttpactionresult> getdepartmentlist()         {             var departments = await repository.getdepartmentlist();             return ok(departments);         }            } } 

however, when call getdepartmentlist() in api, notice repository's context contains no data. shows "enumeration yielded no results" message.

i'm worried i've confused myself when setting solution.

could incorrect connection string? though it's able see database. feel have improper injection of context, not sure or how fix issue.

also, running off local database file (.mdf). should file located? have in dashboard.data folder...should in app_data folder of main project?

edit 1

i've noticed mvc project creates new , empty .mdf file in app_data folder. have connection string in both mvc's web.config app.config in dal

my connection string:

 <connectionstrings>         <add name="nridatabasecontext" connectionstring="data source=(localdb)\v11.0;attachdbfilename=|datadirectory|\nri_database.mdf;integrated security=true;multipleactiveresultsets=true;app=entityframework" providername="system.data.sqlclient" />       </connectionstrings> 

is because of |datadirectory|?

not sure if correct way implement, went ahead , moved .mdf app_data in mvc project, , re-created connection strings point file.

migrations seem work now, data access. correct solution.


Popular posts from this blog