Does IdentityDbContext always need a DefaultConnection - c#

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.

Related

Connection String errors in C# Web Api

I am creating a Web Api in C#.Net. I have implemented Dependency Injection in it using Unity.Mvc5 and using Db First approach. Since then i'm facing some issues in connection strings. There is a default AccountController in it and I have created a TestController to test my Api's.
There are two connection strings in my Web.config file (one of them is commented but just to show here i have uncommented it).
<add name="DDEXEntities" connectionString="Data Source=DESKTOP-CSB6551;initial catalog=DDEX;integrated security=True" providerName="System.Data.SqlClient" />
<add name="DDEXEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-CSB6551;initial catalog=DDEX;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Now the problem is when I use the first string my test controller Api's don't work. The exception given is:
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 when I use the second connection string, my Account controller doesn't work and produces exception:
An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.
My UnityConfig.cs is as follows:
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<DbContext, DDEXEntities>(new PerResolveLifetimeManager());
// register all your components with the container here
// it is NOT necessary to register your controllers
container.RegisterType<AccountController>(new InjectionConstructor());
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<IGenreService, GenreService>(new TransientLifetimeManager());
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
}
}
Can anyone let me know what I have done wrong. This is getting on my nerves now.
Thanks in advance.
Edit + Solution:
Based on Roman's answer, I named my first connection string as DefaultConnection and in ApplicationDbContext class constructor I gave this DefaultConnection. Now my AccountController uses this connection string and all other controllers use second connection string.
I hope it helps someone.
That's because AccountController uses your DbContext in CodeFirst mode. I think it's better to use separate DbContext: first one just for authentication and authorization(in CodeFirst mode) and second one for other stuff(in DatabaseFirst mode).
Also you can try to configure AccountController to use DbContext in DatabaseFirst mode.

Asp.net MVC Identity unable create tables using Database approch

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

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.

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.

C# Setting Entity Framework connectionstring in controller

I'm build my webapi project based on this article: A simple POC using ASP.NET Web API, Entity Framework, Autofac, Cross Domain Support
However, I need to pass a connection string to the DbContext because a user that connects to the webapi can select a different database to work with.
Where and what is that best way to implement this? In the controller, having a separate 'service' or a singleton?
I'm currently building a website that requires multiple databases also. This is the basics (for now). It may not be the best/efficient way, but it does the job so that I can continue working on the site.
Let's say you have this connection and everything there is correct, except the database name can change.
<add name="MyDbContext" connectionString="Server=SomeConnection;Database=SomeDatabase;"providerName="System.Data.SqlClient" />
You can create a connection string on the fly with this
public MyDbContext CreateDbContext(string databaseName)
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings["MyDbContext"].ConnectionString); // Gets the default connection
sqlBuilder.InitialCatalog = databaseName; // Update the database name
return new MyDbContext(sqlBuilder.ToString());
}
Also, unless you want to automatically create a database if it doesn't exist, possibly from incorrect database name, you need to set the initializer. Simply pass the initializer null to prevent ef from automatically creating one. I set this in the constructor of MyDbContext
public class MyDbContext : DbContext
{
/* some other constructors */
public MyDbContext(string nameOrConnectionString, bool createDb)
: base(nameOrConnectionString)
{
if(!createdDb)
{
System.Data.Entity.Database.SetInitializer<MyDbContext>(null);
}
}
}
One way would be to have multiple ConnectionStrings in your web config. You could then select which ConnectionString to use depending on a parameter passed to your webapi:
In your web config:
<connectionStrings>
<add name="dataBase1" connectionString="/*db info here*/" providerName="System.Data.SqlClient" />
<add name="dataBase2" connectionString="/*db info here*/" providerName="System.Data.SqlClient" />
</connectionStrings>
Then in your code:
public someobject GetData(/*arguments*/, string dbType)
{
var connectionString = dbType == 'dataBase1'
? ConfigurationManager.ConnectionString['dataBase1'].toString()
: ConfigurationManager.ConnectionString['dataBase2'].toString()
/*then pass connectionString to dbContext*/
var dbContext = new DbContext(connectionString);
}

Categories

Resources