Programmatically deploy database project in Visual Studio (for testing) - c#

We have a micro service solution that includes:
A SQL Server database project
A Service project
A Test project
We are interested to know if there is a programmatic way, ideally in C# to deploy the database project to a specified target server to enable us to seed and then run a set of API -> database tests. This is to aid both developer and CI/CD testing.
We have explored in memory and SQLite substitution options but ideally want to run tests as representative of production as possible to avoid the potential for the "quirks" of those solutions resulting in problems being missed.

Add a reference to: Microsoft.SqlServer.Dac.
For my usage, I create the UnitTesting database separately as an empty database, then run the Database project against this database.
DacServices instance = new DacServices(connectionString);
String path = Path.GetFullPath(#"..\..\..\..\DatabaseProjectFolder\bin\Debug\DatabaseProject.dacpac");
String databaseName = $"UnitTesting";
using (DacPackage dacPackage = DacPackage.Load(path))
{
instance.Deploy(dacPackage, databaseName);
}

Related

Read Entity Framework "update model from Database" connection string from other source than app.config (Dev branch DB)

My project:
I have a c# data project (currently in TFS, but it will be moved to git) with multiple branches.
Most branches have their own development database (on an azure SQL server)
I am using Entity Framework 6 (Database first with edmx Model, created in the EF4 days)
The database project is used by multiple solutions / starting projects (a hosting project as main project)
I want to automatically have the correct development DB in the EF "Update model from database" dialog. The connection string that should be used must be in the hosting project web.config (the app itself runs on azure, so the web.config connection string is never used during runtime)
Currently I have a process to:
Update the web.config (and all other configs) with a connection string to the specific branch database
I check that change into Source Control
I do a "discard merge" to the main branch (marking the changes as merged without changing the "main" database connection or other dev connections if merging that checkin to other branches)
This process is far from ideal.
I tested with external config files (using the "file" or "configSource" attributes) but that:
Cannot include a file in the parent directory (i would have to create a DatabaseConnection.config file for each project instead of only one central one)
still has the problem if either checking into SC (no real win) or a process to create the file (in the pre-build event). This would require a build before I can update the database (in a new branch / after switching branches when using git?)
So still not ideal.
I did not find anything about the database designer (update model wizard) able to read the connection from somewhere other than the start project config file (?)
I also know that many people have databases per developer and not per branch, but I need the branch databases in this case. I also only have problems with using the correct branch database when updating the model. The correct branch / development database is chosen automatically during runtime (naming convention)
-> Does anybody has any hints about using
Multiple .config files / projects
with one database per branch (mostly)
with often branch switches
or any way to tell the EF6 "Update model from database" wizard where / how to get the connection string from another source
Converting to "Model first" is also not really possible at the moment because of the size of the projects and deploy mechanics

Is code first migration automatically run on the server?

I am very familiar with Entity Framework using Database First approach and how to deploy the solution with it.
Basically, in my solution with database first approach, I have a web client project that consumes data access library project that is coded with database first approach.
So, first, I write some SQL Server scripts to add new tables (or make schema changes).
Next, go to the data access library project, using EF edmx designer to update .net from existing database, compile this data access layer, and the DDL reference is automatically updated in the client web project.
When I deploy the solution to the production server:
First, I need to run the t-SQL scripts on the production SQL server
Next, I deploy the 2 updated DDLs (one for the web and 1 for the data access layer) on the web server.
Now, I have a new application that includes a web project and a data access layer project that uses EF Code First approach.
I am new to EF code first approach. I know any time when I change the database schema, for instance adding a new table, I need to run code first migration in the Package Management Console in Visual Studio to let my back-end database instance change/update.
My question:
When I deploy the application to the production, what are the steps I should follow?
How is the production SQL server updated that is created with EF Code First approach?
Is it a automatic process or I have to run the migration manually like I do inside Visual Studio under the Package Management Console?
Thanks.
If you're using Azure then you can configure it has automatic process as shown below.
Else you have to do it manually like this :
You have to create a db script and after that you can run it on your production db.
PM> Update-Database -Script
You can refer this doc : Getting a SQL Script
Another option where I normaley use :
When I need to run migrations aginst the production db,I change my conn string to reflect production db and after that run :
PM> Update-Database
You have several options with migrations.
You can generate a script using Update-Database -Script (as #Sampath notes)
You can run Update-Database -ConnectionString="YourDbString" and it will do it against the production database for you
You can use a migration initializer and on app startup it will use the applications connection string to run the migration. Do this by putting a line similar to this in your initialization routine:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
Where Context is your DbContext type and Configuration is the configuration class generated when you made the first migration.
See this MSDN article for more information: https://msdn.microsoft.com/en-us/data/jj591621.aspx

Base class not finding app settings

This is somewhat of a general question but I haven't found much by googling it. I have a test framework that sets up an environment for testing purposes. My application consumes this framework through a reference path and runs manual tests just fine. However, once I ask the build server to run my test the framework complains it cannot find any of my app settings. The app.config file sits in my testing project for my application and I am sure it exists in the correct bin folder on my build server. I'm doing this in a C# .NET environment.
EDIT:
I'm not sure what to be more specific about. I would imagine it's something with the build server since it seems to work running tests locally but I have no clue what to look at. Nothing else about the build server is failing, just getting the app settings.
The framework is .NET 4.0 while the main project is 4.5. I'm using nunit to run the tests and running them outside the build process but using the Nunit gui fails at the same point.
The code that grabs the app settings is pretty basic:
string databaseName = ConfigurationManager.AppSettings["databaseName"];
EDIT
Snippet of my test:
public class UserServiceTests : DeployDBEveryFixtureBase
{
public UserServiceTests()
{
DBSetup("Core");
DBSetup("Postal");
DBSetup("Common");
}
private UserService userService = new UserService(string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};",
ConfigurationManager.AppSettings["targetServer"],
ConfigurationManager.AppSettings["databaseName"],
ConfigurationManager.AppSettings["userID"],
ConfigurationManager.AppSettings["password"]));
[Test]
public void UserService_Get()
{
// Act
User user = userService.GetUser(Guid.Empty, "*****", string.Empty);
// Assert
Assert.IsTrue(user.FirstName == "System");
}
}
The environment deployment is in the base class of DeployDBEveryFixtureBase. The DBSetup calls ensure that each database is deployed in the proper order. All of those seem to run fine and my tests complete but I still get the described error. If I look at the database I can all the datbases being properly deployed and then removed (the base class includes a TestFixtureTearDown) but it seems like the build process is trying to run it again.
Note: I am only building the solution file during this process. I do not currently have a .proj file in the build.
Without knowing much about your build server or how exactly you run your tests there is only one issue which comes to mind, that is Shadow Copying. Can you check if your unit tested DLLs are not shadow copied to some location where the app.config file doesn't exist?
Apparently my testing framework had an empty constructor that wasn't so empty. It looks like the app.config was being properly queried but was returning an empty result set since the empty constructor did not have the proper arguments. After removing the contents of the empty constructor my test ran fine. Thank you all for the help.

Setup a test environment on my local machine using Visual Studio

What is the easiest way to setup a test environment on my local machine using Visual Studio 2008 with a website that that has a mssql database, which is hosted through a webhost?
I am web designer and I am re-skinning a live website that is built in .NET C#. I have access to the files and when I run it in Visual Studio (localhost) only the static files are obviously pulling up , which is problematic for testing. I need to be able to run the entire website on my local. I am not going to be messing with the database at all, but I know that I am going to need it to be able to have a local copy that works. I am not extremely savvy on these types of things. I was hoping that someone could either point me in the right direction (ie. search terms, keywords) or give me some instructions on how to make this work. Any help is appreciated.
Thanks.
You should set up MSSQL server first. You can take backup from your production database using
RMC on DB -> Tasks -> Backup..
Then you use the generated .bak file on your local server to insert the copy of the database into your SQL Server instance like this:
RMC on Databases -> Restore Database -> Path to your backup..
At this step your instance should contain the database with all the tables and data. Next thing to do would be to change your web.config file connection string (or any place else, where the "Connection string" is set pointing the website to the database) accordingly to your instance. If it's MSSQLSERVER instance name, you can just use following connection string:
Data Source=(local);Initial Catalog=<database name>;Integrated Security=True;
After this compiling and running your source codes should be returning you the site in its fullest.

sqlite3.dll and system.data.sqlite.dll

Hello people I've been struggling to use sqlite in my C#2.0 application and I have finally decided to get rid of assumptions and ask really basic questions.
When I created a database say iagency with table users, from external tools like firefox plugging and another sqladmin tool I can't query it from sqlicommand inside vs2005 it displays System.Data.SQLite.SQLiteException:Sqlite Error no such table users, please be assured that I've made reference to system.data.sqlite installed with SQLite-1.0.61.0-setup
When I do the opposite like create a database and a table from VS server explorer and VS database gui tools it can't be queried neither but can be seen by other tools, but tables created through query from VS using stringbuilder eg create table bla bla. it can be display in a datagrid but none of the tools can see and display that table.
WHAT DO I NEED EXACTLY TO MAKE SQLITE WORK IN MY APPLICATION?
I've tried to add sqlite3.dll of sqlitedll-3_6_14.zip downloaded from sqlite site under section precompiled binaries for windows as reference to my application but it fails with make sure it's accessible an it's a valid assembly or com component.
I downloaded this SQLite-1.0.61.0-setup.exe Ran the installation then I wrote this to access the firefox favorites sqlite db.
using System.Data.SQLite; // Dont forget to add this to your project references
// If the installation worked you should find it under
// the .Net tab of the "Add Reference"-dialog
namespace sqlite_test
{
class Program
{
static void Main(string[] args)
{
var path_to_db = #"C:\places.sqlite"; // copied here to avoid long path
SQLiteConnection sqlite_connection = new SQLiteConnection("Data Source=" + path_to_db + ";Version=3;New=True;Compress=True;");
SQLiteCommand sqlite_command = sqlite_connection.CreateCommand();
sqlite_connection.Open();
sqlite_command.CommandText = "select * from moz_places";
SQLiteDataReader sqlite_datareader = sqlite_command.ExecuteReader();
while (sqlite_datareader.Read())
{
// Prints out the url field from the table:
System.Console.WriteLine(sqlite_datareader["url"]);
}
}
}
}
Try opening up the database in the command line SQLite tool (from SQLite.org), and check the schema.
You can check the schema in this way:
.schema
This will dump out all the SQL necessary to create the tables in the database. Make sure the table is there, with the name you assume it should have.
You do not need the .dll file from SQLite.org, all you need is the assemblies from System.Data.SQLite.
For me - this link helped a lot at start.
Was harder to get subsonic work, to make database accessible through web application -
but that's another story.
You might try adding the location of the assembly and the db to the Path environment variable. The SQLite assembly contains both .Net and native code merged together, so you do not need the C dll. (the mergebin tool they include to do this is pretty interesting)
I also tried adding the location to Path environment variable but without success.
Finally I copied System.Data.SQLite.dll and System.Data.SQLite.lib into the bin folder of the Web application where other assemblies are located, and application worked.

Categories

Resources