Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:21:25+00:00 2026-06-17T13:21:25+00:00

I want to have ASP.NET MVC website that would have some frontend for looking

  • 0

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:

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);
    }
}

This is part of my controller for frontend:

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
}

This is my repository:

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;
    }
}

This is my HotelManagerContext:

public class HotelManagerContext : DbContext
{
    public HotelManagerContext() : base("name=HotelManagerContext")
    {
    }

    public DbSet<Hotel> hotels { get; set; }
}

Edit:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<IHotelRepository>().To<HotelRepository>();
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
    }    

Edit2:
Here is my connection string:

<add name="HotelManagerContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=HotelManagerContext-20121219191411; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|HotelManagerContext-20121219191411.mdf" providerName="System.Data.SqlClient" />

And I just found that even in HomeController I have empty datacontext. So when I check content of database in Server Explorer there are data which I added (in HomeController). But everytime when I have request page (web api or frontend) datacontext is empty and I can add items there are counting from zero but in database there are already next but can’t get it. It’s really weird.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:21:26+00:00Added an answer on June 17, 2026 at 1:21 pm

    I can’t find solution for this so I create new project and I did everything like in the question. I have same classes, copy and paste code frome question. I can’t find any important difference but second time it’s working.

    I know this isn’t exactly solution but in this case I could start my project from begging so I tried it and it really helps. Just don’t know where is problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC website that connects to a DB via a Web
I have an existing ASP.NET website with some custom routing, within a Solution that
So I have an ASP.NET MVC 3 website that allows people to advertise certain
I have asp.net mvc website and in some pages i need a paging feature,
I have an existing asp.net mvc website that uses basic forms authentication. The site
I have an ASP.NET MVC app and I want to add to each page
I have a Asp.net MVC web application, containing mostly text. I want to put
I am new to ASP.NET MVC 3 and I want to have username/id as
I have created blank Asp.Net-MVC 3 web application and want to write my own
i have just started playing aorund with asp.net mvc and i want to view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.