• Register
0 votes
462 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?

6 5 3
7,540 points

Please log in or register to answer this question.

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")
    {
    }
}
9 7 4
38,600 points
0 votes

Solution:

It become apparent to miss a context instanciation:

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

Must be

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

I was having this similar problem. I’m exercising database first development with an EDMX file.
In case you are employing the connection string produced at the time including the EDMX file in :base(“EDMXConnString”) you will probably have this problem.

I solved this by making a standard connection string that pointed to the database where the ASP.NET define 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 employed that connection string in :base, and it performed

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

My problem was I attempted to use produced  ADO.NET connection string for both originated and authentication context ApplicationDbContext. I fixed it by employing a separate connection string for authentication. Further 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" />

In case you are employing code first, inspect your connection string to confirm providerName is 'SqlClient' as in providerName="System.Data.SqlClient

I have attempted to simulate your problem according with below steps:

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

Result: We could trace user info on local database.

2) Include controller with views employing Entity Framework. And exercise Azure SQL database as its resource.

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

3) Delete default connection string

4) Alter 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();
        }
    }

Follwing above steps, My application give me beneath 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 beneath Migrations folder.

10 6 4
31,120 points

Related questions

0 votes
1 answer 2 views
2 views
Problem: I have an MVC 4 application using entity framework to read and write data to a database I am hosting on an Azure database. The Azure database was supposed to keep the application data AND the login data for the application. ... var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } }
asked Apr 17 ochena 22.1k points
0 votes
1 answer 9 views
9 views
Problem: I am trying to save Employee details, which has references with City. But every time I try to save my contact, which is validated I get the exception: "ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker"
asked Apr 25 ummeshani 8.8k points
0 votes
1 answer 2 views
2 views
Problem: I publish a project developed with an entity framework in a remote server. The pages work fine but when I try to access in my reserved area and so, reading a database, I obtain this error: Unable to find the requested .Net Framework Data Provider. It may not be installed.
asked Apr 25 ummeshani 8.8k points
0 votes
1 answer 3 views
3 views
Problem: I recently upgraded/updated Entity Framework in an old project from version 4 or 5 to version 6. Now I get this exception: An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code. Additional information: ... , deleted all the references to old EF files, and re-installed, but nothing works for me. I keep getting this error.
asked Apr 28 Jack20 5.1k points
0 votes
1 answer 44 views
44 views
Problem: Currently I have the table called Student in the database with some of the values and I am trying to display the values in the webgrid with the help of entityframework. I have done exactly the same kind of work before many times using the Ado.net, but ... I assign the VAR value to List also is this my correct approach? Kindly guide me in fixing my problem related to Entity Framework.
asked Jul 3, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 8 views
8 views
Problem: Need help to be enable to migrate; No context type was found in the assembly enable-migrations.
asked Apr 25 PkGuy 23.5k points
1 vote
1 answer 7 views
7 views
Problem: Any solution to this problem? Enable-migrations no context type was found in the assembly.
asked Apr 25 PkGuy 23.5k points
0 votes
1 answer 73 views
73 views
Problem : I am having my radcombo machines on aspx page and whenever I try to select any option from them then it gives me following `Server Error in '/' Application. Exception Details: System.InvalidOperationException: Operation is not valid due to the current state of the object. My Version Information: Microsoft .NET Framework Version: 2.0.50727.3634 ASP.NET Version: 2.0.50727.3634
asked Dec 2, 2019 alecxe 7.5k points
1 vote
1 answer 8 views
8 views
Problem: Need help with the usage of entities , I am facing this issue. The entity or complex type cannot be constructed in a linq to entities query.
asked Mar 26 PkGuy 23.5k points
0 votes
1 answer 102 views
102 views
Problem : I have one entity type called product which is generated by the entity framework. I have tried to write below query public IMyQueryable<Product> GetProducts(int myCategoryID) {     return from prod in db.Products     where prod.CategoryID== myCategoryID     select ... one select new Product { Name = p.Name}; it miraculously works correctly. How can I perform the custom select section?
asked Jan 27, 2020 jwilliam 3.9k points