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

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 48 views
48 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
0 votes
1 answer 25 views
25 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 Raphael Pacheco 4.9k points
0 votes
1 answer 11 views
11 views
How can you find the mime type of a file based on the file signature, not the extension in .Net? I want a simple way to get a mime type where the file extension is incorrect or not given, something similar?
asked Aug 29 Aliza313 720 points
0 votes
1 answer 44 views
44 views
Problem : I am very new to ASP.Net. I have created my first ASP.Net application which impersonates the user in order to create the AD group, and after that it launches the powershell process as the user saperately. But because of some reason the group ... I would rather not compromise with the security policy with the server ideally, and this application will be used by around 100+ users.
asked Dec 5, 2019 alecxe 7.5k points
0 votes
1 answer 68 views
68 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 jwilliam 3.9k points