Search code examples
asp.netentity-frameworkasp.net-mvc-4ef-code-firstsimplemembership

SimpleMembershipProvider not adding tables to my database


I have a standard ASP.NET MVC4 application, and I am building out the DAL with EF Code First. Currently there are around 20-30 models and I am at a point where I want to integrate users and roles. I have research this a ton and still can't seem to get it to work. Here is what I have right now:

In my database initilization class (Gets called every time I change the model) I seed it with a bunch of data, then call this:

public class DbInit : DropCreateDatabaseIfModelChanges<TrackerContext>
{
    protected override void Seed(TrackerContext context)
    {
      ...seed stuff and save it...
      WebSecurity.InitializeDatabaseConnection("TrackerContext", "User", "Id", "UserName", autoCreateTables: true);
    }
}

Through debugging it does not throw any error and I can confirm it is hitting this line of code. My User model looks like this:

public class User
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public PasswordQuestion PasswordQuestion{ get; set; }
    public string PasswordAnswer { get; set; }
    public string Type { get; set; }
}

From everything I have read, I thought this should be all I need to do to get this working, but I have two problems:

  1. No membership tables are being loaded

  2. My Config seems to be throwing an error even though I am referencing the WebMatrix.WebData dll

Here is the config section:

<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
  <providers>
    <clear/>
    <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
  </providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
  <providers>
    <clear/>
    <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
  </providers>
</membership>

What am I missing here?


Solution

  • As far as I can see, you're missing the

    public DbSet<User> User{ get; set; }
    

    in your context class. You'll have to do this for every table you want Migrations to create for you in your database.