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

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?

Related

How to use localDb without lost Data by using EF in c#

I'm developing a Windows Forms application that I need to storage some data as a database in user's system, so that I would be able to access my database through Entity Framework. I mean I need to copy a database (tables should be created already) from the resources directory to specific directory. Then use it with Entity Framework. To be honest I have no idea. That's my problem!
I make sample fixed problem for all project when create winforms & localdb
You can create 2 ConnectionString in App.config file
App.config
<add name="DevConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" /> <!-- *1* Use when publish project-->
<!--<add name="AppConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='E:\My Projects\MyDatabase.mdf';Integrated Security=True"
providerName="System.Data.SqlClient" />-->
<!--*2* Use for development-->

ASP .NET web api application database error after deployed to Azure

I have an ASP .NET web api project that connects to MS SQL database using entity framework. The project works fine when ran locally.
I published the project on Azure, created a DB in cloud, also added the necessary tables in the DB. After the website is published, everything seems to be fine until I try to access the controller (Eg: mywebsite.azurewebsites.net/api/Groups), I get this error:
I have Enable Migration before publishing my project. When enabling migration, I got an error saying there are two DB: ApplicationDbContext, and myDB (which is where the tables are). So I enabled migration only on myDB. Does the error has anything to do with that?
EDIT:
Here is the connection string from Azure and web.config file:
Web.Config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog=IMS;User Id=userName#xxxxxxxxx;Password=myPassword" providerName="System.Data.SqlClient" />
<add name="IMSEntities" connectionString="metadata=res://*/Models.IMSEntities.csdl|res://*/Models.IMSEntities.ssdl|res://*/Models.IMSEntities.msl;provider=System.Data.SqlClient;provider connection string="Data Source=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog=IMS;User Id=userName#xxxxxxxxx;Password=myPassword"" providerName="System.Data.EntityClient" />
<add name="IMSEntities_DatabasePublish" connectionString="Data Source=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog=IMS;User ID=userName#xxxxxxxxx;Password=myPassword" providerName="System.Data.SqlClient" />
</connectionStrings>
From Azure (All Settings >> Application Settings >> Connection Strings). The name of the connection string in Azure is DbConnectionString
Server=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog=IMS;Persist Security Info=False;User ID=userName#xxxxxxxxx;Password=myPassword;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Thank you for your help!!

EF Migrations not using Web Config connection string

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.

Entity Framework connection string is being rewritten when website is published

When publish a MVC website via Visual Studio it rewrites the connection string for Entity Frameworks with an invalid one.
It appears to be the same invalid one it created when I first imported the database model (not code first) The correct connection string is in my local web.config, and there are no changes being made by the web.release.config file.
Correct connection string:
<add name="Entities" connectionString="metadata=res://*/Models.MODModels.csdl|res://*/Models.MODModels.ssdl|res://*/Models.MODModels.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=R4S-MOD;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Generated connection String:
<add name="Entities" connectionString="Data Source=localhost;Initial Catalog=R4S-MOD;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.EntityClient" />
When you do a release publish they should show you a popup where you choose if you want to do filesystem or iis deployment. there is a section in the wizard which has connection string. check it there, if there is already a preselected value for your connection string – qamar

How to embed database into final c# .exe file?

I have completed my project in c#. I have used SQL Server Compact database (.sdf file). Now, when I compile and run, it works fine. But, how will I be able to run the application in other computers without configuring the connection to database?
In your App.config file add the following after </configSections>:
<connectionStrings>
<add name="NameOfYourConnectionString" connectionString="Data Source=.; AttachDbFilename=|DataDirectory|\student.sdf;Initial Catalog=student; Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Categories

Resources