Data insert issue after migrating database from SQL Azure to SQL Server - entity-framework

I have a database on SQL Azure which has an identity primary.
After using SQL Server Import and Export Wizard, I transferred the data to my SQL Server 2008 R2 database.
My ASP.NET Application runs fine and reads the data. But When I try to insert a value in a table 'User', it gives me an error:
Cannot insert null in column 'UserId'.
The reason being that it is not able to generate the identity value.
How can I overcome this issue?
PS: I tried Generating the scripts from SQL Azure, but the SQL file is 500MB in size and my host does not allow that big a script to run.
Edit: using Entity Framework for data access. The UserId field has an IDENTITY property (1,1).
Edit Tried to create the schema from SQLAzure Migration tool and then used the import/export data to copy the data.
But the wizard does not maintain the relations amongst the rows.

The data import/export wizard doesn't preserve the whole structure of your database objects.
i.e. it will only copy the data, not the whole structure of the table that the data fits into - including identity and key definitions.
You could import the data, and then manually set all the primary keys and default fields to match your desired database definition, or you could connect to your Azure instance and use the generate script option to generate your schema in the 2008 database prior to copying.
But the real answer is that you should be using the Copy Database Wizard to accomplish this, which works fine with Azure. It was designed for this scenario.

The issue was the wizard was trying to insert primary key values, which is disabled by default. And without inserting the primary keys, the relationships can't be maintained, thus the whole issue.
To resolve this issue and do a foolproof migration, ensure that the new schema maintains all the identity columns.
When selecting the source and destination tables, for the specific tables, click on "Edit Mappings" and Check the "Enable identity insert" check box to enable insertion of primary key values, which keep the structure and relations intact.

Related

Idempotency and foreign key constraints when using Postgres pg_dump files with Flyway

I am trying to use some pg_dump generated migration scripts with Flyway. The first migration script is for schema only. The other migration scripts load seed data into various tables using the Postgres COPY command. These seed-data scripts are going to exist as Flyway repeatable migration scripts. This setup presents two issues.
When Flyway loads the seed data from the migration scripts, I'm getting foreign key constraint violations since I don't have the various tables being seeded in the correct order. There are a large number of tables to deal with, so is there an easy way to work around this so that I don't have to try to reorder my COPY's?
Since the seed data is going to be in repeatable migration scripts, these need to be idempotent. Is there a way to do this with the Postgres COPY command? I'm trying to avoid having to convert this to INSERTs since it will hurt performance and also make my migrations files huge.
The trick here for idempotency is to delete the data from the files in the correct dependency order, and when you've done that, to likewise load the data in the correct dependency order. The correct dependency order for deleting the data is worked out by obtaining the target tables for every foreign key constraint and ensuring that no data from a table is ever deleted when it is the target of a table whose data is yet to be deleted. This list of tables in dependency order is usually called a 'manifest' and is required also for CREATE statements and for the PgSQL COPY. The Public domain PowerShell-based Flyway Teamworks framework will create the manifest for you.

How do I change the collation for a CRM 2011 SQL database?

I have a collection of CRM 2011 databases, all of which are installed on the same SQL server running SQL 2008 R2. One of those databases has a collation of Modern_Spanish_Cl_Al, while all of the others are set to Latin1_General_Cl_Al. I want all of them to be set to Latin1, so I would like to go back and fix the Spanish database to be like the others.
How do I change the database collation from Modern_Spanish_Cl_Al to Latin1_General_Cl_Al?
I tried doing it at the database level using the UI, but I received a million errors like:
The statistics 'TeamBase.fndx_Sync_VersionNumber' is dependent on database collation. The database collation cannot be changed if a schema-bound object depends on it. Remove the dependencies on the database collation and then retry the operation.
I tried going to the column level while also using the UI, but received a different error message:
Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created...
I am dealing with this issue in a test environment, so I am willing to experiment, and I realize that what I am trying to do might not be possible without breaking CRM.
Did you try creating a new DB with the correct collation
CREATE DATABASE DatabaseName COLLATE Latin1_General_Cl_Al;
And then Backup-Restore your DB with incorrect collation into the new one?
Should this work, you'll have to import a new organization based on your new database, using the Import Organization Wizard in the Deployment Manager.
edit
After looking into this, it seems quite hard to change collation, even when doing backup/restore.
Your only option if you want to change the collation on the database level seems to be an
ALTER DATABASE ... COLLATE
but that will only affect new objects. You would then have to use ALTER TABLE to change your existing tables.
In addition, because the collation specifies how data is sorted and stored, you need to export all your data and bring it in again (using BCP and BULK INSERT for instance).
To add to this, certain properties of a column prevents its collation from being changed (such as certain constraints and schema bound function references). If you do want to change the collation of the entire database, the easiest way is probably to script the database and create a new with the right collation and then shuffle the data into the new database.

How update database schema from EF5.0 model first changes?

I'm using VS2012 and EF 5.0 with a model first approach. I am wondering if there is any good way to generate incremental DDL to update model changes without dropping all the tables and losing the data I have in there already.
I like to use a SQL server data project within Visual Studio to keep my data in sync with the database - it's like a mini SQL server schema store.
Basically what we are doing here is updating the schema of the data project using the model's DDL script, then comparing and pushing those changes out to the database. Just be sure to generate your model's DDL script first.
Create a new SQL Server Database project
Right click data project and import your existing schema from the database server
Right click data project and import your generated DDL script from model first project.
Right click data project and do a schema compare of your project vs. your database server
Update database based on this schema compare (click update)
Every time you want to update your database just generate and import your models' sql script, compare, and update. It takes a couple steps but works perfectly.

Alter database to match model

Originally, I used Data Modelling in MySQL Workbench to design a database consisting of a series of tables (i.e. the columns and relationships).
Then using Database -> Forward Engineer, I created a database, and inserted data into the tables.
Now I've realised that the model I've designed needs some changes, and so I've altered some tables by inserted columns. My question is, how do I get MySQL Workbench to alter the tables?
Using Database -> Synchronize Model, Update Source just generates a bunch of CREATE TABLE IF NOT EXISTS sql statements, and as the tables exist, nothing changes.
What you are looking for is in the model menu Database / Synchronize model.
As I couldn't get get File -> Export -> Forward Engineer SQL ALTER Script to work, so I made a backup of the data, dropped the tables, recreated them, and then imported the data. I'd rather find a way to get MySQL Workbench to generate ALTER commands from the changes in my model
The 2011 answer is no longer up to date. I struggled to find the option in a recent version. Here is the new procedure (works for MySQLWorkbench 6.2 at least):
When you have finished editing your model, open Database -> Synchronize with Any Source
In the step Select Source you have 3 parts
Source : choose Model Schemadata
Destination : choose Live Database Server
Send updates to : choose whether the live database should be updated or if you only want to saves the changes to a .sql file
Proceed in the wizard, you can then review the tables and sql queries that will be executed. You can also ignore the update of some tables.

How to copy everything except data from one database to another?

In T-SQL (Microsoft SQL 2008), how can I make a new database which will have the same schemas, tables, table columns, indexes, constraints, and foreign keys, but will not contain any data from the original database?
Note: making a full copy, then removing all data is not a solution in my case, since the database is quite big, and such full copy will spend too much time.
See here for instructions: How To Script Out The Whole Database In SQL Server 2005 and SQL Server 2008
In SQL Management Studio, right click on the database and select "Script database as"
http://msdn.microsoft.com/en-us/library/ms178078.aspx
You can then use the script to create an empty one.
Edit : OP did say 2008
I use liquibase for this purpose. Just point liquibase to a different server and it will use your changelog to bring the second database up to date, schema wise. It has the added benefit that the changelog file gets stored in source control and so I can have tagged versions of it, allowing me to restore a database to what a specific version of my app is expecting.