Database Project in Visual Studio

While developing any application we face problem in maintaining database changes. Problem occurs when we want to update existing database or add something new to it or maintaining version control to the changes.
Database comes in handy this place, we can use database projects to create new databases, new data-tier applications (DACs), and to update existing databases and data-tier applications. 
 We can help our development team to manage changes to databases and database servers by creating a database project and putting it under version control.


Steps to Create Database project


Step 1: Open visual studio (I’m using VS 2015) and create a new project.
Step 2: Go to "File" -> "New" -> "Project".
Step 3: Select "SQL Server" in the installed templates.
Step 4: Select "SQL Server Database Project".
Step 5: Enter the Name, choose the location and click "OK"
create new project


In the solution explorer right click and add new table in the solution.


Name the table as customer and click OK.
Once you done with adding table, you will see the default design, you can add columns with datatype there.


Below is the script .

CREATE TABLE [dbo].[Customer]
(
[Id] INT NOT NULL PRIMARY KEY, 
    [Name] NVARCHAR(100) NULL, 
    [Email] NVARCHAR(100) NULL, 
    [DateOfBirth] DATETIME NULL, 
    [Department] NVARCHAR(50) NULL, 
    [IsActive] BIT NOT NULL DEFAULT 0, 
    [CreatedOn] DATETIME NOT NULL DEFAULT getdate()
)


Publish the Database project


Open the project setting by right click on the solution in solution explorer. 


Here you can select the SQL Server version from the drop down.

Select the debug tab under property window.

You can edit the connection here. Click on edit button.
Here you change connection properties and enter server name, user name, password and select database and click OK.

Now right click on solutions and click Publish.

Select the database connection by clicking on edit button.

If everthing is fine success message will come.

Note: Make sure the database version is selected correct otherwise it will give error.

Below is the new table created from using our database project.

Comments

Post a Comment

Popular Posts

How to position controls using Canvas control in WPF using MVVM

Change font and size in visual studio

JWTs in ASP.NET Core 2.0