How can two SQL databases be merged into one database? - sql-server

I have 2 SQL Server databases. The reason is because I created an ASP.NET MVC project using "Individual User Accounts" for authentication. This created a default connection with a database that includes the following tables:
AspNetUserClaims, AspNetUserLogins, AspNetUsers, _MigrationHistory
I then went a step further and built out my own custom roles with a RoleController. So I also have a table for AspNetRoles and AspNetUserRoles. Everything with this is working.
I also have another database that I built in Management Studio that is holding all of the data of my application. I am using Entity Framework to communicate with the database.
I currently reference both databases in my web.config connection strings.
But I would like to migrate the database from the default connection (with my AspNetUsers) into the one in Management Studio.
Is it as simple as copying the tables and putting them in my other database in Management Studio or can I copy the whole schema? Just trying to figure out the best approach here.

You can migrate tables from a database to a diferent one. On SSMS, just right click on a database, select Tasks -> Generate Scripts, and use the wizard to create a script with the table definition and data (optional) to be executed on the new database.

Related

Data Migration from On Prem to Azure SQL (PaaS)

We have an on-prem SQL Server DB (SQL Server 2017 Comp 140) that is about 1.2 TB. We need to do a repeatable migration of just the data to an on cloud SQL (Paas). The on-prem has procedures and functions that do cross DB queries which eliminates the Data Migration Assistant. Many of the tables that we need to migrate are system versioned tables (just to make this more fun). Ideally we would like to move the data into a different schema of a different DB so we can avoid the use of External tables (worried about performance).
Moving the data is just the first step as we also need to do an ETL job on the data to massage it into the new table structure.
We are looking at using ADF but it has trouble with versioned tables unless we turn them off first.
What are other options that we can look and try to be able to do this quickly and repeatedly? Do we need to change to IaaS or use a third party tool? Did we miss options in ADF to handle this?
If I summarize your requirements, you are not just migrating a database to cloud but a complete architecture of your SQL Server, which includes:
1.2 TB of data,
Continuous data migration afterwards,
Procedures and functions for cross DB queries,
Versioned tables
Point 1, 3, and 4 can be done easily by creating and exporting .bacpac file using SQL Server Management Studio (SSMS) from on premises to Azure Blob storage and then importing that file in Azure SQL Database. The .bacpac file that we create in SSMS allows us to include all version tables which we can import at destination database.
Follow this third-party tutorial by sqlshack to migrate data to Azure SQL Database.
The stored procedures can also be moved using SQL Scripts. Follow the below steps:
Go the server in Management Studio
Select the database, right click on it Go to Task.
Select Generate Scripts option under Task
Once its started select the desired stored procedures you want to copy
and create a file of them and then run script from that file to the Azure SQL DB which you can login in SSMS.
The repeatable migration of data is challenging part. You can try it with Change Data Capture (CDC) but I'm not sure that is what exactly your requirement. You can enable the CDC on database level using below command:
Use <databasename>;
EXEC sys.sp_cdc_enable_db;
Refer to know more - https://www.qlik.com/us/change-data-capture/cdc-change-data-capture#:~:text=Change%20data%20capture%20(CDC)%20refers,a%20downstream%20process%20or%20system.

Handling partial database in Visual Studio database project

So we have a SQL Server database that is replicated from a 3rd party vendor's cloud down to our local database. Data in the tables is replicated down continuously from their cloud servers and occasionally they update the full database with new changes to structure. Their schema is the default dbo schema.
For reporting, we've added a second schema (let's call it abc) where we add some additional tables as well as some functions/stored procedures that reference the data in both the abc and dbo schemas. I have set up our abc schema in Visual Studio as a database project and it looks great, however, I can't deploy/publish anything because when it builds, it says it can't find tables that are in the dbo schema. Obviously that's just because we haven't created them in our database project because we've only setup the scripts for our abc schema.
Is there a way to tell Visual Studio to either ignore those errors or to look in the actual database (not the database project) for those tables?
You should be able to create a dacpac of the "base" database, then include that as a database reference using "same database, same server"? That would keep the objects out of your project, but allow you to reference them in your code.

MVC Membership database to SQL Server 2008

When I create a new MVC application with EF, it creates all the views, models, controllers and logic for users to be able to log in, change passwords etc. The data is held in a MDF file in the app_data directory.
I used this for my users, and then had my own SQL Server 2008 database which was created code first using EF for all my other tables. When I wanted to reference a user from a table within this database, I used the ApplicationUser.Id.
Doing it this way, I have not got a foreign key between the table that holds the users and any other table in my custom DB, but thats a different topic.
The question I have, is how I can I stop this happening every time I create a new application, and how do I fix the issue I have now.
I have two different databases, when I just need one. have one database in SQL Server (which is what I want) and one in a file which I cannot convert to SQL Server as it was created in 2012, and I am using 2008 SQL Server.
I am wanting to now deploy this on a server where all data is read from SQL Server.
The way I see it, I need to somehow get all the tables it creates for Membership, and put them into the database I created, then change the connection string. However first off I don't know how I would do this, and second... WHY do I have to do this? How can I just have one database next time. Am I missing something?
After some research and messing around, I have answered my own question and think it may be useful for others.
I am using MVC 5 with the default ASP.NET Identity for my users.
What I found is that if I changed the connection string to point to my SQL server, it will create the database for me (the one that was previously a file in my app directory).
I then added a connection string to the same database for the context I created and it created all the tables for that in the same database.
The result is that I have one database with all my tables in.

Insert rows in to SQL Server using Visual Studio's Server Explorer

I have created a Database in MS SQL and loaded the Database in my Server Explorer. In my MVC application, I wish to simply populate a single entity inside my database (Customer Name for example), does anyone know how I can send data to the database I have loaded? I can't seem to figure it out.
Any help will be highly appreciated.
If you're asking about how to insert data in Visual Studio itself, open Server Explorer, add a Data Connection to your database, expand the Tables node, right click on your table and select Show Table Data. This opens up a screen where you can add data.
If you only want to connect and populate one entity you should consider using Code First Entity Framework to drive your model using a plain C# class on the entity.
Scott Guthrie has a post on how to do that: Using EF Code First with an Existing Database, and Ralph Lavelle has a post that's more specific to MVC and includes migrations in the second part: Using Entity Framework Code First with an Existing Database (1/2) and Using Entity Framework Code First with an Existing Database (2/2).

What determines which local SQL Server cache tables can be cached?

I have a c# Visual Studio app. I connected to an existing SQL Server 2008 database and the Studio created a local cache (.sdf file) for me, caching all existing tables.
Now I added 2 additional tables the the SQL Server database and want to update my DataSet with these, but the wizard does not allow me to select the new tables as being cached. On the "Choose Tables to Cache" wizard page, I cannot check all tables to be cached.
Update: Now it allows me to select one, but not the other of the new tables.
What determines whether tables can be cached? How can I makes this work, with or without the wizard?
Ok, I found the answer.
The "How to: Configure Data Synchronization to Use SQL Server Change Tracking" help page (http://msdn.microsoft.com/en-us/library/cc714038.aspx) talks about creating and configuring a Local Database Cache Template (.sync file).
I never saw the template configuration before because the Studio configured this for me automagically. I found the .sync file in Solution Explorer and can open it in the Configure Data Synchronization dialog. When you add a new table to by synchronized here, there is also a link to the "Which tables can be cached" help page (http://msdn.microsoft.com/en-us/library/bb763049.aspx).

Resources