No context type found in the assembly. ASP.NET MVC

2019-06-09 11:01发布

Was following this tutorial here (wanted a database of customers instead of movies as per tutorial): http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table

However when I attempt to run migrations commands the following error is encountered: "The context type 'MvcCustomer.Models.CustomerDbContext' was not found in the assembly 'MvcVault'."

This is my customer model:

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; }
    }
}

I've tried various combinations of that first migrations command, including:

"Enable-Migrations -ContextTypeName MvcCustomer.Models.CustomerDbContext"

"Enable-Migrations -ContextTypeName MVCCustomer.Models.CustomerDbContext"

Anyway, I'm completely new to all of this and am at a loss. I was able to successfully complete these tutorials when coding up the Movies model following the tutes, and have no idea why its not working if I change the name, etc...

Any help would be appreaciated! Thank you kindly :)

3条回答
Luminary・发光体
2楼-- · 2019-06-09 11:18

In my project, I spell wrong project name, so according the training document, it can't find the class, try just use

Enable-Migrations -ContextTypeName CustomerDBContext

sometimes VS can search it, if this works, you namespace maybe not following the book

查看更多
Summer. ? 凉城
3楼-- · 2019-06-09 11:31

You don't have MvcCustomer namespace. From your code it is clear that you are using MvcVault namespace. Change this:

Enable-Migrations -ContextTypeName MvcCustomer.Models.CustomerDbContext

To this:

Enable-Migrations -ContextTypeName MvcVault.Models.CustomerDbContext

And it should work.

Alternatively, you can change MvcVault to MvcCustomer.

查看更多
不美不萌又怎样
4楼-- · 2019-06-09 11:33

it is late reply but probably it will help someone in future.

Remove connection strings from Web.config IF they are more than one and not needed. Keep only one. Try running default command in Package Manager Console. like Enable-Migrations OR To use auto migrations use this command Enable-Migrations -EnableAutomaticMigrations.

查看更多
登录 后发表回答