-->

ASP.Net Identity customizing UserProfile

2020-07-30 06:30发布

问题:

I'm writing an MVC5 app with EF. I added ASP.NET Identity to the project which created ASPNETUser Tables in my DB the first time I registered a user. However now, when I try to customize UserProfile (as listed in this post: http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on)

I get the following:

The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database

Is it because I already have .edmx file specified (MVC5Entities) => (see 2 connection strings in web.config)?

This is my web.config:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=DILS-S1301;Initial Catalog=MVC5;Integrated Security=True" providerName="System.Data.SqlClient" />
  <add name="MVC5Entities" connectionString="metadata=res://*/Models.Mvc5Model.csdl|res://*/Models.Mvc5Model.ssdl|res://*/Models.Mvc5Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DILS-S1301;initial catalog=MVC5;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

How would I do it?

回答1:

First, I think it does not matter if you have one or more connectionStrings. But you should make sure in the "IdentityModels.cs" file, ApplicationDbContext will use your main connectionString and not the default conncetionString. Like below:

public ApplicationDbContext() : base("MVC5Entities")
{
}

Second, if you added some profile data for the user, you need to update your database using migrations.

1) go to Package Manager Console

2) -> Enable-Migrations

3) -> Add-Migration init

4) -> Update-Database

Hope it helps.