Asp.net MVC Identity unable create tables using Database approch - c#

I am using MVC 5 and sql server 2012.I have created DB with name TestDB its doest not contain any table.Now i am trying to use Identity which is default given in AccountController in MVC project. I have change in AccountController "Defaultconnection" to my TestDBEntities connection but i am getting error
System.InvalidOperationException: The entity type ApplicationUser is not part of the model for the current context. How i can created Identity with exist Database.
<connectionStrings>
<add name="TestDBEntities" connectionString="metadata=res://*/DAL.TestModel1.csdl|res://*/DAL.TestModel1.ssdl|res://*/DAL.TestModel1.msl;provider=System.Data.SqlClient;provider connection string="data source=xxxxx;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("TestDBEntities", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}

As per my understanding, the possible ways to solve your problem is:
1.Cross check that have you added the ApplicationDbContext to the UserStore constructor
Ex: https://stackoverflow.com/a/23894335/3397630
2.Also check that do you have any other connection string in the webconfig file. This may also cause the problem.
Finally if both above points doesn’t work, kindly follow the below article:
http://www.codeguru.com/csharp/.net/net_asp/mvc/securing-asp.net-mvc-applications-with-asp.net-identity.htm
It was really good, I personally used and created a identity tables for my projects without any issues.
In the articles, it was explained each , every points very well and easy understandable.
Hope the above information will be useful , kindly let me know your thoughts or feedbacks
Thanks
Karthik

Related

The entity type ApplicationUser is not part of the model for the current context when querying Identity table database

My project is set up as follows..
MainProject
DataAccess its a Class Library
EntityFramework Database tables its a Class Library
EntityFramework Identity tables its a Class Library
In my main projects webconfig I have 2 connectionstrings, one is for the normal database tables and the other is for the Identity tables. The connectionstring for the normal database tables is an entity framework connectionstring
<add name="HWCEntities" connectionString="metadata=res://*/HWCEF.csdl|res://*/HWCEF.ssdl|res://*/HWCEF.msl;provider=System.Data.SqlClient;provider connection string="data source=****;initial catalog=****;persist security info=True;user id=****;password=****;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
The identity entityframework connectionstring is this
<add name="HWCIdentityEntities" connectionString="metadata=res://*/HWCIdentityEF.csdl|res://*/HWCIdentityEF.ssdl|res://*/HWCIdentityEF.msl;provider=System.Data.SqlClient;provider connection string="data source=****;initial catalog=****;persist security info=True;user id=****;password=****;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
however if I place that connectionstring in the main project's webconfig, and try to log into the web app, I get this error
The entity type ApplicationUser is not part of the model for the current context.
and its pointing at line 78 of AccountController
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
So instead of using the Identity connectionstring, I use this one
<add name="HWCIdentityEntities" connectionString="Data Source=****;Initial Catalog=****;User ID=****;password=****;Integrated Security=false;" providerName="System.Data.SqlClient"/>
using that connectionstring doesn't throw any error, and I would be fine with using that connectionstring but when I try and query the Identity database, I get this error
An exception of type 'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException' occurred in HWC.Identity.dll but was not handled in user code
Additional information: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
and this error is thrown in my HWCIdentityEF.Context.csdl|res
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
How can I fix this so I can query the Identity database? I am assuming that I need to use the Identity connectionstring in the main application?
This works for me:
1) Make sure your context looks similar:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("myConnectString", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
2) Should be only 1 connect string in web.config
3) Tell Identity to use that context instead of IdentityDbContext. Search for IdentityDbContext and replace it with ApplicationDbContext (or whatever you called it). For example:
A) Startup.cs: app.CreatePerOwinContext<ApplicationDbContext>((options, context)=>new ApplicationDbContext());
B) IndentityConfig.cs (user manager and role manager): var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
var manager = new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
After reading solution after solution, and thanks to Steve Greene, I finally figured out what the issue was. I had to change the name of one of the connectionstrings, and changed its context. Now I can login and my queries run.

ASP.NET Identity MVC 5 working locally but throws ApplicationUser error after switch to external database

I've been following this Microsoft Article on implementing a secure web app. It is working fine with the local database. But when I change the connection string to the external database I consistently get an error when trying to interact with the identity tables
AspNetUsers
AspNetRoles
AspNetUserClaims
AspNetUserLogins
AspNetUserRoles
These tables have been re-created in the external Azure db and I followed this walkthrough. The error I get when trying to register is
The entity type ApplicationUser is not part of the model for the current context.
When trying to register it fails on this line of code in the AccountController
var user = await UserManager.FindByNameAsync(model.Email);
I want to take a database first approach as the external db already has pre-existing data. I've looked at several other posts and sites but I've been unable to solve the issue. I've also tried enabling and updating the db with no success through the Package Manager Console.
Thank you in advance!
Here is the ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
//ApplicationDbContext.Equals("", "");
return new ApplicationDbContext();
}
public System.Data.Entity.DbSet<proj.Models.Obj1> Obj3 { get; set; }
public System.Data.Entity.DbSet<proj.Models.Obj2> Obj3 { get; set; }
public System.Data.Entity.DbSet<proj.Models.Obj3> Obj3 { get; set; }
}
I don't like answering my own question but here is what resolved my problem.
I had to change the connection string from the one that was generated
<add name="DefaultConnection" connectionString="metadata=res://*/Models.DBName.csdl|res://*/Models.DBName.ssdl|res://*/Models.DBName.msl;provider=System.Data.SqlClient;provider connection string="data source=server;initial catalog=databaseName;user id=username;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
to this
<add name="DefaultConnection" connectionString="Data Source=server; Initial Catalog=databaseName; User ID=username; Password=password; Connect Timeout=60;" providerName="System.Data.SqlClient" />
I was using the connection string generated during the creation of the EDMX file, database first. Using this connection string was causing my error. I found it a bit deceiving because I was still able to access data from other tables but not the associated identity tables. I hope this will be helpful to anyone experiencing the same issue in the future.

Modifying default connection string to add users VS2013 project - The entity type ApplicationUser is not part of the model for the current context

I've created a new C# MVC project in VS2013 and am trying to use their automatically generated user creation function.
It seems in the IdentityModel.cs file, there is a method called ApplicationDbContext as shown below with the "DefaultConnection" string.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Now this seems like its using VS's in built database as shown in the Web.Config file
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-AI-20150321012806.mdf;Initial Catalog=aspnet-AI-20150321012806;Integrated Security=True" providerName="System.Data.SqlClient" />
I'm wanting to use my own database connection string instead while using the in built user creation functions (as the in built user creation seems to have all the fields i need for user creation, and handles password hashing and such), but I can't seem to get the user creation to work when i change the "DefaultConnection" string to my own connection string as defined in the Web.Config file.
<add name="AIEntities" connectionString="metadata=res://*/Models.AIEntities.csdl|res://*/Models.AIEntities.ssdl|res://*/Models.AIEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=PC;initial catalog=AI;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Every time when i try to create a new user, i get the error message:
The entity type ApplicationUser is not part of the model for the current context.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The entity type ApplicationUser is not part of the model for the current context.
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
What is needed to get the user creation control to work exactly how it would do while changing the connection string?
Well the name of your connection is AIEntities before it was DefaultConnection. In your ApplicationDbContext constructor rename your DefaultConnection to AIEntities
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("AIEntities", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
The ApplicationDBContext then passes that string to connect
Wow this is very interesting, I came across this article:
http://patrickdesjardins.com/blog/the-entity-type-applicationuser-is-not-part-of-the-model-for-the-current-context which seems to describe the problem i'm getting, although their "answer" doesn't work for me (and also for the others who have also commented that it doesn't work for them either).
However after reading "Matt"s comment:
Matt
February 11, 2014 at 03:31
I was having this same problem, and I recall having a similar problem working with SimpleMembership in MVC4.
I’m doing database first development, so I have an EDMX file. Turns out, ASP.NET Identity does not like the connection string that is created when you generate your .edmx model file. If you are using a. EDM connection string in :base(“EDMConnString”) 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 (in my case the same database), used that connection string in :base, and it worked.
I tried doing the same thing and created a new ConnectionString as follows (basically removed everything that got auto generated when i added a new model):
<add name="AI2" connectionString="data source=PC;integrated security=True;initial catalog=AI;" providerName="System.Data.SqlClient"/>
And under the IdentityModels.cs file, set my connection string to AI2:
public ApplicationDbContext()
: base("AI2", throwIfV1Schema: false)
{
}
And this seems to have solved my problems, it's doing exactly what the template code was doing on its local DB onto my own DB.

Does IdentityDbContext always need a DefaultConnection

I was working at an asp.net MVC project with the IdentityDbContext.
The code for the context:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("ApplicationDb")
{
}
}
And in my web.config a connectionstring named after the context:
<add name="ApplicationDb" connectionString="Data Source="..." providerName="System.Data.SqlClient" />
Strange thing is when I call the Update-Database command to create the database with Entity Framework, my database is created. So far so good.
But: the authorisation code from Owin is also creating a second upon running the application. This one is named DefaultConnection and is copy from the other one.
So my question: does Identity Framework always need a connection string named "DefaultConnection", even if you point the context to another connectionstring?
In the end I managed to solve this by adding the DefaultConnection connectionstring in web.config so I end up with two connectionstring:
ApplicationDb
DefaultConnection
Is this really the way to go? Because if that's the case it doesn't make much sense to put a custom connectionstring name in the base constructor?!
Btw, I also tried the context like so:
public ApplicationDbContext()
{
}
Which in theory should effectively do the same. But still DefaultConnection is created upon running the app. Doesn't make sense to me.

ASP.NET MVC 5 Identity with Model First

I'm developing a web application and I would like to implement Identity. I've done a couple of tests and it's pretty easy to implement when using a Code First approach.
My issue is that I can't get it to work with a Model First approach. I would like to define my models in the .edmx file, and generate my database using the "Generate database from model" option.
Here's what I've done:
I started from the default ASP .NET Web Application template, including the "Individual User Account" authentication. Then I registered a user in order for entity framework to generate the needed tables in the default database.
Then I added the "ADO .NET Entity Data Model" to my project, and chose the "EF Designer from Database" option. It has been generated successfully with the existing tables created by Identity. I changed the connection string in the IdentityModels:
public ApplicationDbContext()
: base("MyConnectionStringName", throwIfV1Schema: false)
{
}
But after this, when I try to register a user, I get the following error:
"Unable to load the specified metadata resource."
Previously, I also had an error: "The entity type ApplicationUser is not part of the model for the current context."
Is it actually possible to use Identity with a Model First approach ? If yes, what am I doing wrong ?
I said I'll post something in order to explain how I managed to get this working. I struggled to find some documentation and tutorials about using Identity with a Model First approach, so here it comes.
Firstly, I found this genius article from Ben Foster: http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1
This article discuss about how to implement an "Identity Stripped Bare". Reading it and following the steps allowed me understand how to use identity from scratch (you can see at the beggining that he generates an MVC 5 project with no authentication system).
If you want the code of this "Naked Identity", here it is: https://github.com/benfoster/NakedIdentity
The first step was to generate the database. In the Naked Identity code, the v1.0 of Identity is used. Some things are different (a couple table properties for example), but it stays mostly identical.
To create a database usable by Identity, I simply ran the template MVC 5 project, registered a user for the table to be created with the Code First approach, and then copied those tables needed by Identity in my empty database.
Once this was done, I generated my .edmx using the freshly created database. A connection string is added to the web.config. A connection string may already exist as "DefaultConnection". You need to keep this one as well.
Basically, those 2 connection strings are needed because, Identity is gonna use the default one, and the .edmx is gonna use the other one. It is not possible to use the same one for both as the .edmx connection string needs metadata, that are not supported by identity. Thus, those 2 connection strings are different, but I modified them so they can refer to the same database.
<connectionStrings>
<add name="DefaultConnectionString" connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=DatabaseName; Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="EntitiesConnectionString" connectionString="metadata=res://*/Models.WebAppModel.csdl|res://*/Models.WebAppModel.ssdl|res://*/Models.WebAppModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDb)\v11.0;initial catalog=DatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
The first connection string is the one you're gonna use with entity. So here is how the IdentityDbContext is initialized:
``
public class AppDbContext : IdentityDbContext
{
public AppDbContext() : base("QualityWebAppDbEntitiesDefault", throwIfV1Schema: false)
{
}
public static AppDbContext Create()
{
return new AppDbContext();
}
}
``
(you can find the AppUser definition in the NakedIdentity sources)
And for my .edmx, the initialization looks like this:
public partial class QualityWebAppDbEntities : DbContext
{
public QualityWebAppDbEntities()
: base("name=QualityWebAppDbEntities")
{
}
}
Hope this will help people that want to use a Model First approach with Identity.

Categories

Resources