This is from a tutorial. I have two models, Restaurant
and RestaurantReview
.
Restaurant
is like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OdeToFood.Models
{
public class Restaurant
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Country { get; set; }
public ICollection<RestaurantReview> Reviews { get; set; }
}
}
And RestaurantReview
is like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OdeToFood.Models
{
public class RestaurantReview
{
public int Id { get; set; }
public int Rating { get; set; }
public string Body { get; set; }
public int RestaurantId { get; set; }
}
}
Now I've created the class OdeToFoodDB
:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace OdeToFood.Models
{
public class OdeToFoodDB : DbContext
{
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<RestaurantReview> Reviews { get; set; }
}
}
Now I am trying to create a Microsoft SQL Server database. I went to Server Explorer -> add Connection and chose Microsoft SQL Server. According to the tutorial one of the possible database names should be OdeToFood.Models.OdeToFoodDB
. But it is not available as a choice:
When I try to type OdeToFood.Models.OdeToFoodDB
in it says
The database OdeToFood.Models.OdeToFoodDB does not exist or you do not have permission to see it.
I'm using Microsoft Visual Studio 2013 and Microsoft .NET framework 4.5.5
Update in response to Ivan:
I have created a _db
in the HomeController
like this:
namespace OdeToFood.Controllers
{
public class HomeController : Controller
{
OdeToFoodDB _db = new OdeToFoodDB();
public ActionResult Index()
{
var model = _db.Restaurants.ToList();
return View(model);
}
}
Update 2: Here is my connection strings section of web.config
:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-OdeToFood-20160318150024;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-OdeToFood-20160318150024.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>