【问题标题】:No context type found in the assembly. ASP.NET MVC4在程序集中找不到上下文类型。 ASP.NET MVC4
【发布时间】:2013-10-20 09:47:16
【问题描述】:

在此处关注本教程(根据教程需要一个客户数据库而不是电影): http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table

但是,当我尝试运行迁移命令时,遇到以下错误: “在程序集 '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<Customer> Customers { get; set; }
    }
}

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

“启用迁移-ContextTypeName MvcCustomer.Models.CustomerDbContext”

“启用迁移 -ContextTypeName MVCCustomer.Models.CustomerDbContext”

无论如何,我对这一切都是全新的,不知所措。在按照 tutes 编写 Movies 模型时,我能够成功完成这些教程,并且不知道为什么如果我更改名称等它不起作用......

任何帮助将不胜感激!谢谢你:)

【问题讨论】:

  • 只需清理并重建项目以将更改应用于命名空间。再次执行命令。它对我有用,希望能帮助别人

标签: c# asp.net asp.net-mvc-4 .net-assembly entity-framework-migrations


【解决方案1】:

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

Enable-Migrations -ContextTypeName MvcCustomer.Models.CustomerDbContext

到这里:

Enable-Migrations -ContextTypeName MvcVault.Models.CustomerDbContext

它应该可以工作。

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

【讨论】:

    【解决方案2】:

    回复晚了,但以后可能会对某人有所帮助。

    如果连接字符串不止一个且不需要,则从 Web.config 中删除它们。只保留一个。 尝试在包管理器控制台中运行默认命令。 喜欢Enable-Migrations 或 要使用自动迁移,请使用此命令 Enable-Migrations -EnableAutomaticMigrations

    【讨论】:

      【解决方案3】:

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

      Enable-Migrations -ContextTypeName CustomerDBContext

      有时 VS 可以搜索它,如果可行,您的命名空间可能没有遵循本书

      【讨论】:

        猜你喜欢
        • 2015-11-23
        • 2012-10-13
        • 2020-04-19
        • 2013-05-06
        • 2021-08-31
        • 2011-12-11
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多