简体   繁体   English

实体框架不在数据库中创建表

[英]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. 我刚刚开始使用一些教程来学习ASP.Net MVC和实体框架,但我遇到了问题。

I created class for Database Connection OdeToFoodDb.cs 我为数据库连接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 还有两个类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 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 而且我有数据库,但是它不包含表RestaurantReviews或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 检查您的连接字符串是否指向您期望的数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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