public class FoobarContext : IdentityDbContext<ApplicationUser>, IFoobarContext
{
public FoobarContext() : base("connectionStringNameFoobar") {...}
...
}
This is my DbContext I am using in my solution. I have a web application, which at some point accesses the context and it can successfully connect and login to my database.
Now I have also added a console application to my solution. It uses the same code. It tries to initialize the same Context. But when I run my console application, I get the exception that the user cannot login because the provided password is wrong.
Here is also the sql server log "Login failed for user 'FooBarUser'. Reason: Password did not match that for the login provided. [CLIENT: local machine]"
Here is my connection string:
<connectionStrings>
<add name="connectionStringNameFoobar" connectionString="data source=localhost;initial catalog=MyFoobarDB;User Id=FooBarUser;Password=FooBar123;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
When I enter the connection string directly it does not works either. e.g.:
public FoobarContext() : base("data source=localhost;initial catalog=MyFoobarDB;User Id=FooBarUser;Password=FooBar123;MultipleActiveResultSets=True;App=EntityFramework") {...}
The strange thing is that when I use an overload for IdentityDbContext ctor and pass an existing SqlConnection, it works. e.g.:
public FoobarContext () : base(new SqlConnection(ConfigurationManager.ConnectionStrings["connectionStringNameFoobar"].ConnectionString), true)
Note that I have also tried passing the connection string via the configuration manager as seen above and not using the overload, where I pass a SqlConnection but that resulted in the same login error.
Additionally:
- I have checked my connection strings several times. They are identical in my web app and in my console app.
- I have checked them with another colleague.
- I have checked that the connection string gets resolved correctly
- I can login manually into the db (through management studio) when using the credentials from the connection string
My Setup:
- .NetFramework 4.6.1
- EntityFramework 6.1.3 (but I have also tried it with 6.2.0
- Autofac 3.5.2
In each case (web app & console app) the FoobarContext is instantiated via Autofac.
So this begs the question: Why?
EDIT: passing the connection string directly also DOES NOT work. My bad