How to use localDb with entity framework migrations? - c#

I have Persistence class library, which contains my DbContext class. It also contains app.config file, where I have predefined connection string like this:
<connectionStrings>
<add name="<Namespace1>.Persistence.AssessmentContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\AssessmentContext.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Assuming that AssesmentContext has Namespace1.Persistence namespace.
When I try to enable-migrations using PM console, it gives me such an error:
An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file.
When I tried to debug it and put debug output into AssessmentContext ctor I found that the connection string is still using .\SQLEXPRESS data source.
Data Source=.\SQLEXPRESS;Initial Catalog=<Namespace1>.Persistence.AssessmentContext;Integrated Security=True;MultipleActiveResultSets=True
So what I am doing wrong? And why EF doesn't take my connection string from app.config?

The above connection string entry must be copied to the app.config of the executable program if you are using desktop app or copied to web.config if you are using web app.

Related

Entity Framework Migration cant find connection string in App.config file

I have a second project in my solution called DataAccess (.NET Framework Class Library) for models. I set my main asp .net mvc project as start up and from package manager console I set DataAccess as default. I already have an existing database. So I installed Entity Framework in DataAccess layer using package-install command to get the latest. Then I added an Entity Data Model. But whenever I try to run enable-migrations from PM it shows that No connection string named 'MAHMHEntities' could be found in the application config file. Although there is a connectionString in App.config called MAHMHEntities. In my MAHMHEntities.cs class I have declared the connection string:
namespace DataAccess.Entities
{
using System.Data.Entity;
public partial class MAHMHEntities : DbContext
{
public MAHMHEntities()
: base("name=MAHMHEntities")
{
}
//other dbset classes
}
}
In DataAccess layer I have App.config file that has two connectionString, I added the DefaultConnection myself :
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQL2014;Initial Catalog=MAHMH;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="MAHMHEntities" connectionString="data source=.\SQL2014;initial catalog=MAHMH;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
Whichever connection I use I always get the same error when I try to enable migration. Entity Framework version is 6.2.0.
You need to set Data access project as start up project. Or you can add your connection string to web application's(Which is currently start up project) web.config

Entity Framework connection string without password

before I was using a connection string with integrated security.
Now I switched it to username/password and created a new user in my SQL server.
I generated a new connection string with the server explorer in visual studio by adding a new connection (I didnt really added a connection there, I just used the wizard and copied the connection string for the app.config).
then I had to update-database and it worked.
But now I recognized that there is no password in my new connection string.
How does it work? I can't find the password anywhere...
There are a couple ways:
1: Add the string to the constructor of your Context.
public class Context: DbContext
{
public SchoolDBContext() : base("connectionstring")
{
}
}
2: webconfig
<configuration>
<connectionStrings>
<add name="putYourEntityName" connectionString="putYourConnectionStringHere" providerName="System.Data.EntityClient"/>
</connectionStrings>
</configuration>
As referenced from here: Entity Framework Connection String in App.Config

Unable to load the specified metadata resource. On code first approach

Hi i am trying to create the tables using code first approach on EF5. I have created a database called "DEV_Database" on a server called "SERVER01".
After I enter "enable-migrations -contexttypename Entities -force" on package manager console I get this error "Unable to load the specified metadata resource."
I had this connection String auto generated for me by VS12, which words fine with other approaches.
Can anyone help me with this. My connection string is below.
<add name="Entities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=SERVER01;initial catalog=DEV_Database;persist security info=True;user id=USER1;password=pass;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>

Entity Framework not using connection string in app.config

My error message states
The underlying provider failed on Open
and the InnerException states that the database cannot open by the requested login.
The strange thing is that my connectionString is showing a different login than what's mentioned in the error. Why is the application using an incorrect connectionString than the one that's explicitly defined in my App.Config?
Few common reasons why:
1) You aren't using that project as your StartUp Project
2) You didn't match the name in the config
<connectionStrings>
<add name="YourContext"... >
</connectionStrings>
3) You are hard coding it in the YourContext.cs

How to connect to the connection string written in the web.config of the web services

We have a web service project called 'Service' and in the web.config of the 'service' I have set the connection string as follows:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=L308;Initial Catalog=Dashboard;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I am trying to access the connection string from another project 'DBConnector' using the following code but getting null reference exception even though after adding the reference of the 'Service' into 'DBConnector'. using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
You will need to have the connection string in DBConnector project config file. Adding reference to a project doesn't bring in the config of that project in the main config of the project
If i got you
You can not do that if you want to use the Connection string of service in you library. You need to expose that as property or method
in you service.cs
public string connectionstring
{
get
{
return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
}
}
or
DBConnetor added as referance
If the DBconnector has added as referance in the Service layer than you can access it easily by the code you have written
If you need want to make that connection string Global so all you applications and services can use it you will need to put it in the machine.config.
The machine config if found here %WinDir%\Microsoft.NET\Framework\\CONFIG.
'Service' into 'DBConnector'.
Seems to be incorrect thing to do. It should have been other-way around!

Categories

Resources