简体   繁体   中英

Entity Framework doesn't create tables in Database

I've just started to learn ASP.Net MVC and Entity Framework using some tutorial and I have a problem.

I created class for Database Connection OdeToFoodDb.cs

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

and two classes Restaurant.cs

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> Review { get; set; }
    }
}

and RestaurantReview.cs

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

Then I try Add Connection 在此处输入图片说明

在此处输入图片说明

And I've got database but it doesn't contain tables RestaurantReviews or Restaurants

在此处输入图片说明

but my tutorial says that I get these tables automatically. What I did wrong? How to make it create tables I need?

In your Package Manager Console, run the following command

Update-Database -ProjectName <YourProject.DataLayer>

Check if your connection string is pointing to your expected database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM