0

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?

ledgeJumper
  • 3,560
  • 14
  • 45
  • 92

2 Answers2

0

1) Did you check your ConnectionString? Should be something like this:

 <connectionStrings>
    <add name="DefaultConnection" connectionString="data source=SERVER;initial catalog=DATABASE;user id=USER;password=PASSWORD;" providerName="System.Data.SqlClient" />
</connectionStrings>

Furthermore you should have this in your config too:

 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>

2) Check your references. System.Web.Webpages should be there and CopyLocal should be set to true. Then check your .NET Framework Version as well (4.0, no Client Edition)

Hope this helps.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
John Thompson
  • 160
  • 1
  • 9
0

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.

Yustme
  • 6,125
  • 22
  • 75
  • 104
  • True that you want you Classes in you DbContext class you create, this is where migrations kicks in and creates those tables, but for this particular issue I am talking about the tables that are created with the Simple Membership Provider. Those tables are created when you call the line WebSecurity.InitializeDatabaseConnection(...). This is kind of an older question so I don't know what my exact problem was here, but now I typically put that line in my global.asax, seperate from migrations, and it works fine. – ledgeJumper May 07 '13 at 13:52
  • So you're talking about the tables that Simple Membership Provider includes in the database? I can point you to 2 tutorials where this is discussed. I've had a similar issue that those tables didn't got created, because i had to create them manually. But i'm glad to read you got it solved. – Yustme May 07 '13 at 16:38