EF Migrations not using Web Config connection string - c#

I have my web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SRV;Initial Catalog=SomeCatalog;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I have my web.debug.config:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SRV;Initial Catalog=SomeCatalog;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(DefaultConnection)"/>
</connectionStrings>
I have my web.defaultserver.config
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\;Initial Catalog=EverybodyPilates;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(DefaultConnection)" />
</connectionStrings>
When I run the migrations:
> Update-Database -Force -Verbose
It hangs for ages because it can't seem to find my connection string. Am I doing my web config correctly?
Or have I got something else incorrect?

It's interesting that it hangs rather than returning an error. By default it will pull connection information from the startup project. You can specify the projects manually:
Update-Database –SourceMigration $InitialDatabase -ProjectName ProjectWithMigrations -StartUpProjectName WebApp
You can also force your context to use that connection string, by default it will look for one with based on the context name.
public class MyContext : DbContext
{
static MyContext()
{
}
public MyContext()
: base("Name=DefaultConnection")
{
}
}
If none of that seems to help you might try right clicking on your web app and choosing publish, and do a simple file system publish in Debug build configuration and manually inspect the web.config to see if it transformed properly and even throw it in wwwroot, if there are connection string issues you should get an exception.

Related

How to check for DbContext class when running command using NuGet on Visual Studio?

enter image description hereI am running this command on the package-manager console, but I can't see whether it has executed well. Where do I check if it executed well? Here is my command on the package-manager console?
scaffold-DBContext "Data Source="(LocalDb)\MSSQLLocalDB; Catalog=eNtsaOnlineRegistrationDB;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer-OutputDir Models
Here is my connectionString below
<connectionStrings>
<add name="eNtsaOnlineRegistrationDB" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\eNtsaOnlineRegistrationDB.mdf;Initial Catalog=eNtsaOnlineRegistration; Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Update-Database for multiple projects, with only one context?

I have a solution with two projects:
MyProject.Database
MyProject.Database.Test.
Both projects contain an app.config, where a the connectionstring is specified as such:
<connectionStrings>
<add name="TrackerDB"
connectionString="Host=hostName; user id=userName;
password=myPassword; database=TrackerDB" providerName="Npgsql" />
</connectionStrings>
and
<connectionStrings>
<add name="TrackerDB"
connectionString="Host=hostName; user id=userName;
password=myPassword; database=TrackerTestDB" providerName="Npgsql" />
</connectionStrings>
(Notice the difference at the database attribute).
With this setup all my tests will run its own database, with generated data, and a database for the real data runs separately.
Whenever I do a Update-Database I set the project with the DbContext (MyProject.Database), but this will not update MyProject.Database.Test database.
What should I do to automatically update both databases?

Reading value from Web.Config that uses configSource files

In keeping with the recommendation here we separated our connection string into an xml file. Our web.config:
//old - works
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;"
providerName="System.Data.SqlClient" />
<connectionStrings>
//new - fails
<connectionStrings configSource="connections.config" />
content of connections.config:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Both files are located in the project root folder. EntityFramework works fine with both solutions. However when I try to obtain the connection string for some tests it fails to retrieve.This is the code that we use to extract the connection string:
string ConnectionString =
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
Reading the connection string direct from web.config works, when we introduce the ConfigSource it fails. What do we need to make it work
You need to set the Copy to Output Directory property of your configuration file to either Copy always or Copy if newer.
If you don't do this, your ConnectionStrings.config file will not be copied to the output debug/release folder and so will not be found.
Also note that the external file should have the same name as of the section name. Like it should be connectionStrings.config, not connections.config

Cannot Access To ASP.NET Configuration Tool

When I want to access to ASP.NET Configuration Tool it gives me the following error message:
There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.
The following message may help in diagnosing the problem: Unable to connect to SQL Server database.
What can I do. Is there any configuration which I should set before I can access or ?
I am using ASP.Net MVC4 Project with build in connection string.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-AccountPortal-20131128183241;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-AccountPortal-20131128183241.mdf" providerName="System.Data.SqlClient" />
you should access your connection string from web.config file before proceeding further.
web.config file :
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-AccountPortal-20131128183241;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-AccountPortal-20131128183241.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
from code : access connection string which is in web.config file
string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

mvc3 razor first run database connection error

I am working on my 1st mvc razor project. I set the project and on the 1st run I am getting an error that the _appstart.cshtml page can't connect to the database.
I can see the db in the app_data folder and on this line of code I am getting error:
WebSecurity.InitializeDatabaseConnection("StarterSite", "UserProfile", "UserId", "Email", true);
This is my 1st time working on mvc razor project, and every advice is welcome about what should I enter there.
UPDATE:
conn string in web.config:
<connectionStrings>
<add name="StarterSite" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\StarterSite.mdf;User instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
Make sure your connection string is pointing to the correct location (database). You can find it in your Web.config file in your solution.
e.g.
<connectionStrings>
<add name="WebContext" connectionString="Data Source=server.local;Initial Catalog=blops.test;Persist Security Info=True;User ID=Userid;Password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>

Categories

Resources