Programmers learn & share
0 votes
47 views

Problem :

Currently I am migrating from the Identity 1.0.0 to Identity 2.0.1.

But the migrations code generated is nothing about the new IdentityUser. And It does not even add the new columns.

So I tried to make the new project and also I tried again but my migrations codes are empty.

To fix that problem I tried to do the edits directly in my SQL Server and imported the database again in the solution.

But when I want to register the user I face the following exception :

The entity type ApplicationUser is not part of the model for the current context

At below line

IdentityResult result = await UserManager.CreateAsync(user, model.Password);

 

Any clue about the solution on my above problem?

by (7.5k points)   | 47 views

2 Answers

0 votes

Solution :

I was facing the same problem. I was doing database first development with my EDMX file.
If you are also using your connection string generated when adding your EDMX file in :base(“EDMXConnString”) then you will also most likely face this problem.

I fixed this by simply creating the standard connection string that was pointed to the database where my ASP.NET Identity tables are as follows:

<add name="MyConnString" connectionString="MyData Source=server; Initial Catalog=db_name; User ID=,myuser_id; Password=mypassword; Connect Timeout=60;" providerName="System.Data.SqlClient" />

And then I used that connection string in the :base, and it simply worked!

public class MyApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public MyApplicationDbContext()
        : base("MyConnString")
    {
    }
}
by (36.1k points)  
0 votes

Solution:

for me it seems to miss a context instanciation:

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());

should be

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

I was having this same problem. I’m doing database first development with an EDMX file.
If you are using the connection string generated when adding the EDMX file in :base(“EDMXConnString”) you will most likely have this problem.

I fixed this by creating a standard connection string that pointed to the database where the ASP.NET Identity tables are.

<add name="MyConnString" connectionString="Data Source=server; Initial Catalog=db_name; User ID=user_id; Password=password; Connect Timeout=60;" providerName="System.Data.SqlClient" />

And then used that connection string in :base, and it worked!

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

My problem was I tried to use generated ADO.NET connection string for both generated and authentication context ApplicationDbContext. I fixed it by using a separate connection string for authentication. Also pay attention to the provider - for authentication context it has to be System.Data.SqlClient:

<add name="DefaultConnection" connectionString="Server=qadb.myserver.com;Database=mydb;User Id=myuser;Password=mypass;" providerName="System.Data.SqlClient" />

If you are using code first, check your connection string to ensure providerName is 'SqlClient' as in providerName="System.Data.SqlClient

If you are using database first, check your connection string to ensure providerName is 'EntityClient' as in providerName="System.Data.EntityClient

I have tried to reproduce your issue according with below steps:

1) create Asp.net MVC template, then register a new user.

Result: We could find user info on local database.

2) Add controller with views using Entity Framework. And use Azure SQL database as its resource.

Result: we will find two connection in our web.config

3) Delete default connection string

4) Change Application DB Context connection string

<add name="jambdbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=tcp:jambdb.database.windows.net,1433;initial catalog=jambdb;user id=jambor;password=***;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("jambdbEntities", throwIfV1Schema: false)
        {
        }

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

After above steps, My application give me below error:

To Solve this issue

1) Edit 'DefaultConnection' connection string

 <connectionStrings>
    <add name="jambdbEntitiesapplication"   providerName="System.Data.SqlClient" connectionString="Server=tcp:jambdb.database.windows.net,1433;Initial Catalog=jambdb;Persist Security Info=False;User ID=jambor;Password=***;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" />
    <add name="jambdbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=tcp:jambdb.database.windows.net,1433;initial catalog=jambdb;user id=jambor;password=***;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

2) Modify the code:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("jambdbEntitiesapplication", throwIfV1Schema: false)
        {
        }

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

 

3) Modify AutomaticMigrationsEnabled = true; in Configuration class under Migrations folder.

 

ago by (10k points)  
2,218 questions
2,683 answers
59 comments
241 users