[英]Entity Framework creating database with no tables in it
我正在创建一个ASP.NET MVC Web应用程序。 我已经安装了实体框架。 我已经完成了以下模型:
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; }
}
我创建了以下DbContext类:
public class EmploymentDbContext : DbContext
{
public DbSet<EmploymentHistory> EmploymentsHistory { get; set; }
}
我的Web.config文件中的连接字符串如下所示:
<connectionStrings>
<add name="EmploymentDbContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=EmploymentsHistory;Integrated Security=False" providerName="System.Data.SqlClient"/>
</connectionStrings>
运行项目时,可以在Visual Studio的服务器资源管理器中看到数据库EmploymentsHistory
,但是当我展开它,然后展开它的Tables时,我看不到应该看到的EmploymentHistory
表。
尝试向您的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; }
}
通过调用DbContext
的构造函数,您可以指定要将此DbContext
此子类附加到的连接字符串的名称。
编辑:
由于您似乎目前没有尝试在任何地方使用此连接,因此请尝试显式添加代码优先的迁移。 如果尚未安装,请在Package Manager控制台中运行enable-migrations
。 关闭显示的“配置”屏幕,然后运行add-migration EmploymentHistory
。 等待它完成,然后运行update-database
,看看是否能解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.