\Web Api and Frontend database

avengr

New Member
I want to have ASP.NET MVC website that would have some frontend for looking into, adding and other things with data. And then I want to have Web Api for getting data for mobile devices and so. I want to use code first from EF (and I am using Ninject). I create classes for hotel and others. I created HotelManagerContext and database is created and looks good. I am adding data by HomeController and by Repository for this. When I looked at database in Visual Studio data are there. But when I tried to use my HotelsController for Api datacontext is empty. What's wrong? What I forget to set? Something with connection string or what?This is my ApiController:\[code\]public class HotelsController : ApiController{ private IHotelRepository _hotelRepo { get; set; } public HotelsController(IHotelRepository repo) { _hotelRepo = repo; } // GET api/Hotels public IEnumerable<Hotel> GetHotels() { return _hotelRepo.GetAll(); } // GET api/Hotels/5 public Hotel Gethotel(int id) { return _hotelRepo.Get(id); }}\[/code\]This is part of my controller for frontend:\[code\]public class HomeController : Controller{ private IHotelRepository _hotelRepo { get; set; } public HomeController(IHotelRepository repo) { _hotelRepo = repo; } public ActionResult Index() { return View(); } // next methods, for adding data and so}\[/code\]This is my repository:\[code\]public class HotelRepository : IHotelRepository{ private HotelManagerContext db { get; set; } public HotelRepository() :this (new HotelManagerContext()) { } public HotelRepository(HotelManagerContext hotelManagerContext) { // TODO: Complete member initialization this.db = hotelManagerContext; } public Models.Hotel Get(int id) { return db.hotels.SingleOrDefault(h => h.hotId == id); } public IQueryable<Models.Hotel> GetAll() { return db.hotels; } public Hotel Add(Hotel hotel) { db.hotels.Add(hotel); db.SaveChanges(); return hotel; }}\[/code\]This is my HotelManagerContext:\[code\]public class HotelManagerContext : DbContext{ public HotelManagerContext() : base("name=HotelManagerContext") { } public DbSet<Hotel> hotels { get; set; }}\[/code\]Edit:\[code\] private static void RegisterServices(IKernel kernel) { kernel.Bind<IHotelRepository>().To<HotelRepository>(); GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel); } \[/code\]
 
Top