0

I am doing an online course on MVC.Net that as a very superficial component of the Entity Framework.

So far this project consists of 2 classes (Movies and Customers), and associated Views and Controllers.

I am stuck because I am unable to replicate the Entity Framework component of the project.

Here are the steps, what I expected (based on the result on the course) and what I got:

1 - Create migration project

Steps: Nuget Package Manager > Type "enable-migrations"

Expected result: Success message. A folder "Migrations" created in the solution.

Actual result: Error. "enable-migrations is not recognized as the name of a cmdlet."

After investigating, I noticed that I didn't have the Entity Framework installed, so I installed it using NuGet. After that, the above command worked.

2 - Add migration

Steps: on the console, write "add-migration InitialModel"

Expected result: Sql commands are added to the "Migrations" folder. "IdentityModels" class added to the "Models" folder with some authentication methods.

Actual result: Error "No context type was found in the assembly". Basically nothing happened after that.

I found an article explaining that there has to be a class in the project that inherits from DbContext. So I did just that. Created an empty class that inherits from "DbContext". The command above now works, but the IdentityModels class was not created, no scrips were added, no authentication methods were created.

What am I missing? Why did I have to create this class inheriting from "DbContext" when the course video didn't have to do that? And finally why aren't these authentication classes and methods being automatically created the same way I saw on the course?

I am asking these questions because I really hope this issue will help me understand the Entity Framework a bit better.

1
  • I'd recommend you follow another online course that's dedicated to EF because you probably need more background than can be given appropriately in a Stack Overflow answer. If you insist on asking here you should include an MCVE. Commented May 4, 2018 at 21:25

1 Answer 1

1

Assuming you are using Identity to create tables to store user data,etc. There are a few pieces to the puzzle you'll want here:

A class that inherits from IdentityDbContext (not just DbContext since you want the Identity tables as well):

 public class MyContext: IdentityDbContext<ApplicationUser>
  {
    /// <summary>
    /// default project context
    /// </summary>
    public MyContext() : base("yourConnectionString")
    {
      //optional but I use: Configuration.LazyLoadingEnabled = false;
      //optional but I use: Configuration.ProxyCreationEnabled = false;
    }

    public static MyContext Create()
    {
      return new MyContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);

      //remove cascade deletions; we handle our deletions manually in the code
      //optional but I use: modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
      //optional but I use: modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
    }

    //add a dbset for each model class that you want to make a table for
    //you do NOT need to do this for the Identity provided classes
    public DbSet<Movie> Movies { get; set; }
    public DbSet<Customer> Custoemrs { get; set; }
  }

a web.config entry for your connection string (note dependending on your db, the connectionString="..." section of this may be different for you):

<configuration>
  <configSections>
    <connectionStrings>
      <add name="yourConnectionString" connectionString="Data Source=localhost;Initial Catalog=ProEdVerificationPortalDb;Integrated Security=True;" providerName="System.Data.SqlClient" />
    </connectionStrings>
  </configSections>
</configuration>

A class to represent your application users:

public class ApplicationUser : IdentityUser
{
   //only application user fields that are specific to your application
   //i.e: any fields that the default AspNetUsers table does not have
   public DateTime? InactiveDate { get; set; }
}

Depending on how you have migrations set up, this may just generate a migration but not actually run it. You may need to type command update-database into the package manager console after you've added the migration so that the migration is actually run.

Sign up to request clarification or add additional context in comments.

1 Comment

This works! But obviously something failed along the MVC course I am following. It did not require these classes to be manually added to the project (the "add-migration" somehow did everything automatically) and the "update-database" command added a MDF file to the project (which didn't happen with the solution given here). However, I have the tables created in a SQL Server instance, which is good enough for me to proceed with the course. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.