简体   繁体   English

实体框架创建没有表的数据库

[英]Entity Framework creating database with no tables in it

I'm creating an ASP.NET MVC web application. 我正在创建一个ASP.NET MVC Web应用程序。 I've installed the Entity Framework. 我已经安装了实体框架。 I have done the following model: 我已经完成了以下模型:

public class EmploymentHistory
{
    public Guid Id { get; set; }
    public Guid UserId { get; set; }
    public int DateFromMonth { get; set; }
    public int DateFromYear { get; set; }
    public int DateToMonth { get; set; }
    public int DateToYear { get; set; }
    public string CompanyName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string County { get; set; }
    [DisplayName("I was unemployed during this time")]
    public bool IsUnemployed { get; set; }
}

I have created the following DbContext class: 我创建了以下DbContext类:

public class EmploymentDbContext : DbContext
{
    public DbSet<EmploymentHistory> EmploymentsHistory { get; set; }
}

And the connection string in my Web.config file looks like that: 我的Web.config文件中的连接字符串如下所示:

<connectionStrings>
<add name="EmploymentDbContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=EmploymentsHistory;Integrated Security=False" providerName="System.Data.SqlClient"/>
</connectionStrings>

When I run the project, I can see the database EmploymentsHistory in the Server Explorer in Visual Studio but when I expand it and then expand its Tables, I can't see the EmploymentHistory table which I'm supposed to see. 运行项目时,可以在Visual Studio的服务器资源管理器中看到数据库EmploymentsHistory ,但是当我展开它,然后展开它的Tables时,我看不到应该看到的EmploymentHistory表。

Try adding a constructor to your EmploymentDbContext class, so it would be like this: 尝试向您的EmploymentDbContext类添加一个构造函数,因此如下所示:

public class EmploymentDbContext : DbContext
{
    // constructor, which will also call DbContext's (base) constructor
    public EmploymentDbContext() : base("name=EmploymentDbContext") { } 

    public DbSet<EmploymentHistory> EmploymentsHistory { get; set; }
}

By calling DbContext 's constructor, you're specifying the name of the connection string you'd like this subclass of DbContext to attach to. 通过调用DbContext的构造函数,您可以指定要将此DbContext此子类附加到的连接字符串的名称。

Edit: 编辑:

Since you don't seem to be trying to use this connection anywhere currently, try explicitly adding a code-first migration. 由于您似乎目前没有尝试在任何地方使用此连接,因此请尝试显式添加代码优先的迁移。 If you haven't already, run enable-migrations in your Package Manager Console. 如果尚未安装,请在Package Manager控制台中运行enable-migrations Close out of the Configuration screen it presents, and then run add-migration EmploymentHistory . 关闭显示的“配置”屏幕,然后运行add-migration EmploymentHistory Wait until it's finished, then run update-database and see if that fixes it. 等待它完成,然后运行update-database ,看看是否能解决问题。

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

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