在程序集中找不到上下文类型。 ASP.NET MVC4

在这里遵循这个教程(根据教程需要一个客户数据库而不是电影): http : //www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a -新场到了,电影模式和表

但是,当我尝试运行迁移命令时,会遇到以下错误:“在程序集’MvcVault’中找不到上下文类型’MvcCustomer.Models.CustomerDbContext’。”

这是我的客户模型:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; namespace MvcVault.Models { public class Customer { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime Born { get; set; } public int Telephone { get; set; } public string Email { get; set; } } public class CustomerDBContext : DbContext { public DbSet Customers { get; set; } } } 

我尝试过第一次迁移命令的各种组合,包括:

“Enable-Migrations -ContextTypeName MvcCustomer.Models.CustomerDbContext”

“Enable-Migrations -ContextTypeName MVCCustomer.Models.CustomerDbContext”

无论如何,我对这一切都是全新的,我不知所措。 当我在Tute之后对Movies模型进行编码时,我能够成功地完成这些教程,并且如果我更改名称等,则不知道为什么它不起作用…

任何帮助都会被贬低! 非常感谢你 :)

您没有MvcCustomer命名空间。 从您的代码中可以清楚地看到您正在使用MvcVault命名空间。 改变这个:

 Enable-Migrations -ContextTypeName MvcCustomer.Models.CustomerDbContext 

对此:

 Enable-Migrations -ContextTypeName MvcVault.Models.CustomerDbContext 

它应该工作。

或者,您可以将MvcVault更改为MvcCustomer。

这是迟到的回复,但可能会在将来帮助某人。

从Web.config中删除连接字符串,如果它们不止一个且不需要。 只保留一个。 尝试在Package Manager Console中运行default命令。 如Enable-Migrations或要使用自动迁移,请使用此命令Enable-Migrations -EnableAutomaticMigrations

在我的项目中,我拼写错误的项目名称,所以根据培训文档,它找不到类,尝试只使用

 Enable-Migrations -ContextTypeName CustomerDBContext