how do I save my database tables and values? - asp.net

I have an asp.net mvc website, and I would like to know if/how to save my database tables and values to my migrations/models so when I publish my website the first time it runs it creates the database on the iis sql server and fills the tables with the data? I am using sql server 2016 on my computer and on the server. right now it creates a default database on first run but no tables get created. I have just been making the database and tables manually when I publish, but I want to know if its possible to have it done automatically.

Related

Copy some entries from one server database to other server database using asp.net mvc

We are doing some CRUD operation in DEV environment and data is saved in database. For other environments(like Staging/Prod) we want to copy those records from DEV database and paste to Staging/Prod when required using asp.net MVC. Is it possible? Could you please suggest some pathway in order to accomplish this?
You can accomplish this using a linked server:
https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/create-linked-servers-sql-server-database-engine?view=sql-server-ver15
This involves creating a link between one database server and another (they must be able to see each other on the network). You can then reference the linked server like this:
INSERT INTO [LinkedServerName].[Database].[dbo].[Table] (......
Alternatively you could use a paid for tool like SSMS tools which can generate insert statements on a per table basis: https://www.ssmstoolspack.com/

SQL Server Data Tools - How do I preserve data along with schema?

I have been using a SQL Server project to manager the structure of a database.
First I created the project, then imported a database.
Then, when I need to make a schema change, say change a field name, I do it in the SQL Server Project, and then publish it to the actual database using the Schema Compare Option.
I would like to take this a step further to hold basic data that the database needs. Say for example a OrderType table with 2 records "PHONE", "WEB"
This data will be needed on all new instances of the database. Is there a way to keep these in the SQL Server project as well so that they don't get lost?
It seems the only way to do this now, is to keep an actual copy of the master database with the metadata in it, and then use a data-compare. But it would be great if the data could just get published at the same time as the schema so the resulting database is complete.
There are two ways to preserve static data and publish it with a database.
Have a "reference" database with static data populated. At the time of publishing a new instance, SQL Server Data tools has a "Data Compare" tool which allows you to compare to live databases, and creates a custom script to update one database with data from the other.
Create scripts that contain insert statements, and then run these scripts at publish time. SQL Server Data tools has two tools to assist in this.
a. Open the data table (right click on SQL Server object explorer, and select "View Data"), and then click on the "Script" button at the top. It will create an insert script for all rows in the table. More on Comparing Data from MSDN
b. Then take this created script, and add it to the Database Project as a "Post Deployment" script. When you create a publish script for the database, any Post Deployment scripts in the project are automatically included in the master script. More on post deployment scripts from MSDN

Dynamic Data to external database asp.net

I'm stepping through a walk through which allows me to create a dynamic data website.
Though in all the tutorials, including the one above, they all point to a local file based database. I need to add an external data model.
How could I add an external data model to the project? (A database hosted on a seperate SQL Server)
I've tried connecting through the SQL Server Object Explorer then dragging it into App_Data, but that didn't work.
Use the connection string in the web.config, but as you add the EF data model you should be given the opportunity to select you database by browsing for it.

Importing data into new database configuration

I had a website, with an sql server database. I decided to create a new version of the site, so I downloaded the database + website onto my local dev PC, and added a whole bunch of stuff to both - in particular, I added lots of new stored procedures, columns and tables to the database, while leaving the existing data for the site in place while doing this.
It is now time to launch the new version. Of course, while working on the new version, the data in the database on the live site has changed - new users have signed up and so on, so I can't just push the dev enviroment database live, as this would lose data.
What is the best way to import all the data from the existing database into the new database configuration? Should I take the existing database, and then add all the columns, procs, tables, indexes and so on in to it, or is there a better way?
You can use SQL Compare or other comparison tools to make the production database look like your dev database. If budget is a concern you can see plenty of alternatives in this blog post.
In SQL Server Management Studio , right click on your local database -> Task -> Generate Scripts, and then you'll be able to select your SP/Functions and then execute these script against the production database

Updating SQL Server database with SQL scripts

I have a number of manually written scripts (.sql) for tables, views and stored procedures that are used from an ASP.NET application. These scripts drop the object and recreates them. I need a way to update the database when the scripts change without deleting the object. For example, when a column is added to an existing table that has rows in it, I would need to update this table with this extra column without losing the rows.
I need a way to "update" the database on a single click (I can hook up the changes using a batch file). Does Visual Studio support this kind of functionality?
If you get Visual Studio Team System - Database Edition 2008 - which is now bundled with "Developer Edition" for free - it handles that. Visual Studio database projects without that edition really just store the static SQL that you want to track. The Database Edition is capable of determining the 'deltas' between your SQL and what's in a target database, generating that script, and executing against your database. You do get the option of reviewing that generated SQL, but by default it is very safe [it won't run if it thinks that there will be any data lost].
Yes - it's called Database Projects.
You can define a Visual Studio Database Projects, have create and change SQL scripts inside it, and then execute those against a database connection of your choice when you need to.
See this blog post here for a great explanation, or read the whole series that the 4 guys from Rolla wrote.

Resources