How to rename database and files with entity framework - c#

I'm building a patch-application that modifies an existing MS SQL database. There are multiple steps the user goes through during the patch, but no changes are made till the end.
The program uses Entity Framework in order to read and write data to the database, and i'm collecting all changes within a single transaction so that i can rollback any time.
However, now i need to be able to rename the database on the SQL Server (and it's files)... Is this achievable with Entity Framework, to be contained within the same transaction as the other operations? If yes, how? And will the connection be broken once the database is renamed?

EF is not able to do that. Use SMO, it has Database.Rename method and it is much more appropriate tool to work with db schema:
Server srv = new Server(conn);
Database database = srv.Databases["AdventureWorks"];
database.Rename("newName");

Related

How would I merge two database i.e one created in plain old sql server and other in Entity framework?

I have a database Database named 'DatabaseEF' and another database 'DatabaseOld'.
The former is created in EntityFramework and later is a plain old database created directly in sql server.
Now we have been tasked with merging these 2 but let the old one stay the same way and should not be in EF context because it is also accessed by old ado.net drivers etc.
So how should I go about it?
As for the schema, scaffold (generate entities from existing database, aka. db-first) the old database, combine them into the EF context, and make some fixes if there are something not compatible with the new context or logically incorrect.
As for the existing data on production environment, apply the migration with ef and execute raw sql commands to transfer the data.

What changes are needed to switch database from SQL Server to Oracle using Entity Framework?

I have developed a MVC web application using Entity Framework with SQL Server as backend. I have used a database-first approach.
Now my client wants to switch to an Oracle database. I am in the process of doing impact analysis of switching from SQL Server to Oracle database. My client believes that it is as simple as changing connection string.
My question is, considering the fact that the table name, table structure, relationships and attributes are all same with SQL Server database, what changes do I need to do in my project to switch from MS SQL to Oracle?
Do I need to create a new edmx file for Oracle database or do I only need to change the connection string and use the same context?
Also if in future this needs to be reversed, then is there a good practice or solution to support both the databases?
Thank you.
oracle has a 30 character limit for item (table, index, column) so if you are verbose in any of your naming you are going to have to rename a bunch of items.
i don't like using an edmx. it might be easier with code first models.

Entity framework creating duplicate tables

I have inherited an entity framework application that i have been tasked to create within a separate environment with a completely separated database as well on a new db server. Seems easy enough. I backup the database from the original and restore it on the new database server. the data is the exact same at this point.
For testing purposes i change the local connection string to the new database server and run my web app locally. I get an error in the browser that is "MySql.Data.MySqlClient.MySqlException: Can't write; duplicate key in table '#sql-3b87_2d5f91'" i dont even have a table with that name and have no idea where it comes from. entity framework also creates duplicate tables for all of my tables except for migrations table.
I have tried other things as well, after restoring the db in the new db server to the original again, i have tried running 'update-database' and the same issues happen.
Now if i disregard restoring the new database with the backup from the original and run "update-database" on the new database server. it creates the schema correctly, but it lacks the data i need for testing.
Any ideas why this may be happening? i would like to avoid writing a sql script to transfer data.

Sync Framework - Syncing data without schema changes

Is there a way to use Microsoft Sync Framework without implementing the required schema changes ('_tracking tables'). Basically, I am faced with the task of Syncing two SQL Server 2008 databases, one of which is a legacy db that we cannot make any schema changes to.
Would it be possible to store the additional tables required for each database in a separate database?
e.g. I have 3 tables that we need to sync (Staff, Customer & Sales), normally we would just add the three additional tracking tables, but this isn't possible. Instead, can I have a separate database with the required tracking tables (Staff_tracking, Customer_tracking, Sales_tracking) and somehow point the sync framework to this new db??
Any help is appreciated, and a code example would be super!
Since you are using SQL 2008 as the database, just turn on change tracking and let SQL Server track the change tables for you internally without having to change the schema of the actual client database. MSDN explains it nicely in this article. About half way down you will see the following:
SQL Server 2008 has introduced a new alternative method for tracking
changes called SQL Server 2008 Change Tracking. The concept behind
change tracking is that an administrator marks certain tables to be
monitored for changes. From that point SQL Server 2008 keeps tracks of
any inserts, updates, or deletes that are made. When a remote
“requestor” requests changes, SQL Server 2008 will provide all of the
changes that have occurred since the last successful download as
specified by the requestor. The Sync Framework database
synchronization providers have been built to take advantage of SQL
Server 2008 change tracking and provide the following advantages for
an OCA environment:
No schema changes are required to be able to track changes.
Assuming you are using the standard Microsoft synchronization providers, change tracking support is included by default.

Modifying a SQLite database schema and data via SubSonic 3 within a transaction

I'm using Visual Studio 2008, C#, SQLite via System.Data.SQLite with SubSonic 3. My application has gotten to a state where I need to upgrade the database schema (add columns, triggers, etc) then add new records via SubSonic generated ActiveRecord objects - all within the scope of a single transaction so that any failures could be nicely rolled back.
Ideally, this is sort of what I was hoping to do:
Begin Transaction
try
Update Schema to latest version
Use SubSonic objects to add new records/modify existing
Commit Transaction
catch
Rollback Transaction
Unfortunately, this doesn't work. All the schema changes are currently being via SubSonic.Query.CodingHorror, which doesn't seem to respect transactions. Is there a way to perform both schema changes and SubSonic data changes within the scope of a single transaction?
I think I found the answer to my question: use the same database connection for all actions and use transaction on that one database connection. Since I hadn't told SubSonic how to handle all the database connections for me - it used the default method, one connection per lookup or created object or CodingHorror. Since transactions can't span database connections, the behavior I saw was fully expected.
Once I created my own database connection and did the lookups, creates and CodingHorror schema changes on it, all transaction stuff begin to work properly.

Categories