Partho Rocko
Partho Rocko

Reputation: 73

EF not creating identity table when trying to create new database

I have 2 model classes:

  1. Customer.cs with name and Id
  2. Movies.cs with name and Id

I tried to run enable-migrations, but I got this error:

No context type was found in the assembly WebApplication2'.

Then I saw some answers on websites and people told to make a DBContext class. I do not have any DBContext class as I just made a new MVC project. So, I tried to make a DbContext class of my own as follows:

{
    public class MyDBContext:DbContext
    { 
        public void MyDbContext()
        {
        }
    }
}

Then I was able to run enable-migrtaions command and Migration folder was created with configuration.cs as follows:

internal sealed class Configuration : DbMigrationsConfiguration<WebApplication2.Models.MyDBContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(WebApplication2.Models.MyDBContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data.
        }
    }
}

Now when I run the add-migration Initialmodel the Up() and Down() methods are empty and there are no Identity tables. Please help !

Upvotes: 0

Views: 934

Answers (1)

Bil Simser
Bil Simser

Reputation: 1723

First off I suggest you refer to creating a new MVC project using Entity Framework. There are a lot of tutorials but here's the Microsoft one which is accurate and pretty complete:

Get Started with Entity Framework 6 Code First using MVC 5

It also includes a section on Migrations, however you don't need Migrations until you have a database and a model that's changing.

I would suggest backing out your Migrations until we're ready for them. Rick Strahl has a good article on how to back them out and get back to a clean state:

Resetting Entity Framework Migrations to a clean State

Finally, your DbContext class has to have a DbSet. A DbSet class is an entity set that can be used for create, read, update, and delete operations. With your DbContext class as it is, Entity Framework has no idea what to do or map.

Change your DbContext class to something like this:

{
public class MyDBContext:DbContext
{ 
    public void MyDbContext()
    {
    }

    public virtual DbSet<Movie> Movies {get; set;}
    public virtual DbSet<Customer> Customers {get; set;}
}

This will allow you (say in a Controller) to do something like this to add a new Customer to the database:

var customer = new Customer { name = "John Smith" };
using(var context = new MyDbContext())
{
   context.Customers.Add(customer); // adds the customer to the DbSet in memory
   context.SaveChanges(); // commits the changes to the database
}

NOTE: I don't recommend creating a DbContext this way in a controller, in the first link on using EF6 with MVC 5 there are better ways.

Hope that helps.

Upvotes: 1

Related Questions