exception after upgrade ASP.NET Identity to 2.0

2019-03-19 03:03发布

问题:

my project: VS2013, Entity Framework, Web forms, database first, Identity

I updated all NuGet packages of my project today (2014-4-15). Among them, Identity is upgraded to 2.0.0.0.

I thought things were going good, but unfortunately when I run the application, the following statement gives an exception.

namespace xxx.Models
{
    // You can add User data for the user by adding more properties to your User class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("MyConnection") 
        {
        }
    }
...
}

The exception information is as follows. It asks me to do Code First Migration. But my project is a Database First webforms project. How can I solve this problem? Thanks!


An exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNet.Identity.EntityFramework.dll but was not handled in user code

Additional information: The model backing the 'ApplicationDbContext' context has changed since the database was created. This could have happened because the model used by ASP.NET Identity Framework has changed or the model being used in your application has changed. To resolve this issue, you need to update your database. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=301867).
Before you update your database using Code First Migrations, please disable the schema consistency check for ASP.NET Identity by setting throwIfV1Schema = false in the constructor of your ApplicationDbContext in your application. public ApplicationDbContext() : base("ApplicationServices", throwIfV1Schema:false)

回答1:

You need to disable the schema consistency by doing what the error says. This is one time thing that happens when you upgrade from version 1.0 to 2.0.

public ApplicationDbContext() : base("MyConnection", throwIfV1Schema:false)

Next step - do the migrations.

Everything should work after that and you can remove this throwIfV1Schema:false

You can also take a look at this for more info



回答2:

The problem is here :

public class ApplicationUser : IdentityUser
{
}

I think you should change to partial class to extend entity in Entity Framework. The reason is that EF will generate proxy class for each entity to connect to database.

The partial class should be write in the same namespace.