I have several installations of my Linq-to-Sql app running in the field. Now I've created a new version, which adds a new column to a certain table. I've added this column in the dbml file. But when updating the installation, I want to preserve the existing database. How to handle this? Linq-to-SQL doesn't seem to like this inconsistency.
Is there an easy way to update the existing database using my new dbml file?
You need to manage your database schema explicitly - that is to say that you should have creation and update of the database schema scripted so that its repeatable. For the scenario your describing I think that your application should (ideally) create and then update the database schema as required. The initial work to set this up isn't too hard and once you have the system in place making schema changes is straightforward
I wrote this up (in terms of what has worked for me for a lot of years now) at some length here:
How to create "embedded" SQL 2008 database file if it doesn't exist?
Which probably ought to be modified to take advantage of this which talks about using database extended properties:
SQL Server Database schema versioning and update
Related
I'm using SQLite and Entity Framework code-first. And my Windows desktop application needs the ability to Create and Open files.
For creating a database, I found DbContext.Database.Migrate(), which will create the database if needed, and them make the schema current.
For opening a database, I'm not as certain. I may try to automatically updated it, but I might also prompt the user before doing so.
I found DbContext.Database.GetMigrations(), but how would I use that to determine if the database file is already current?
I also found DbContext.Database.GetAppliedMigrations() and DbContext.Database.GetPendingMigrations().
DbContext.Database.GetPendingMigrations() can be used to get all the migrations that are not applied to the database.
I've hit a wall when it comes to adding a new entity object (a regular SQL table) to the Data Context using LINQ-to-SQL. This isn't regarding the drag-and-drop method that is cited regularly across many other threads. This method has worked repeatedly without issue.
The end goal is relatively simple. I need to find a way to add a table that gets created during runtime via stored procedure to the current Data Context of the LINQ-to-SQL dbml file. I'll then need to be able to use the regular LINQ query methods/extension methods (InsertOnSubmit(), DeleteOnSubmit(), Where(), Contains(), FirstOrDefault(), etc...) on this new table object through the existing Data Context. Essentially, I need to find a way to procedurally create the code that would otherwise be automatically generated when you do use the drag-and-drop method during development (when the application isn't running), but have it generate this same code while the application is running via command and/or event trigger.
More Detail
There's one table that gets used a lot and, over the course of an entire year, collects many thousands of rows. Each row contains a timestamp and this table needs to be divided into multiple tables based on the year that the row was added.
Current Solution (using one table)
Single table with tens of thousands of rows which are constantly queried against.
Table is added to Data Context during development using drag-and-drop, so there are no additional coding issues
Significant performance decrease over time
Goals (using multiple tables)
(Complete) While the application is running, use C# code to check if a table for the current year already exists. If it does, no action is taken. If not, a new table gets created using a stored procedure with the current year as a prefix on the table name (2017_TableName, 2018_TableName, 2019_TableName, and so on...).
(Incomplete) While the application is still running, add the newly created table to the active LINQ-to-SQL Data Context (the same code that would otherwise be added using drag-and-drop during development).
(Incomplete) Run regular LINQ queries against the newly added table.
Final Thoughts
Other than the above, my only other concern is how to write the C# code that references a table that may or may not already exist. Is it possible to use a variable in place of the standard 'DB_DataContext.2019_TableName' methodology in order to actually get the table's data into a UI control? Is there a way to simply create an Enumerable of all the tables where the name is prefixed with a year and then select the most current table?
From what I've read so far, the most likely solution seems to involve the use of a SQL add-on like SQLMetal or Huagati which (based solely from what I've read) will generate the code I need during runtime and update the corresponding dbml file. I have no experience using these types of add-ons, so any additional insight into these would be appreciated.
Lastly, I've seen some references to LINQ-to-Entities and/or LINQ-to-Objects. Would these be the components I'm looking for?
Thanks for reading through a rather lengthy first post. Any comments/criticisms are welcome.
The simplest way to achieve what you want is to redirect in SQL Server, and leave your client code alone. At design-time create your L2S Data Context, or EF DbContex referencing a database with only a single table. Then at run-time substitue a view or synonym for that table that points to the "current year" table.
HOWEVER this should not be necessary in the first place. SQL Server supports partitioning, so you can store all the data in a physically separate data structures, but have a single logical table. And SQL Server supports columnstore tables, which can compress and store many millions of rows with excellent performance.
I am developing a C# app where I have to read/write existing MS SQL database. I decided to use object class for the database but the table columns can be changed during runtime and that causes an exception because of an attempt to write a new row (in the case of a new not null column).
Is there any recommendation how to preserve object approach to the database and deal with variable database tables? It is not necessary to have the object updated in the runtime, just to handle the new columns - fill them with a valid default value.
More details to my solution:
I used Data Source Configuration Wizard in VS2015 what generates objects for the database and everything is fine. When a table has a new column I have to run the wizard again to update the objects and define appropriate new value.
I can't modify anything in the database structure (existing ERP system). The database is huge (hundreds of tables, each has around 60+ columns) so I am looking for the automated ways how to generate the database objects.
I hope I just overlooked (as a newbie) some obvious solution.
Thanks for all suggestions in advance.
Petr
I would recommend to do the following:
Create a set of import tables with the needed columns and leave those tables fixed
Let your application copy data to the import tables
Update the production tables on the database from the import tables with a stored procedure
I have a MVC4 + EntityFramework Database First application. I have made some changes in my local database (added table and columns in couple of tables). After this I updated my .edmx file and ran the custom tool. This has updated my models of the table whose schema i have changed. Everything is working fine.
I want to know, How to reflect those local database changes on my Test database?
Within VS you can compare the two databases and generate a change script that will bring them on par with each other.
In your case you want to do a schema compare.
Tools -> SQL Server -> New Schema Comparison.
You select your local database and then select the test database. it will compare the schema of both and show you the differences. You can select which you want to apply and either apply it directly or generate a change script and execute when you want from SSMS.
Compare and Synchronize Database Schemas
In SQL Server Management Studio you can export the database script to drop and recreate the tables, indexes, relationships, etc... You can also use Visual Studio to export the SQL script to create all the tables. The other way is to manually add the updated columns. I'm unsure of any other ways to do this in a database first approach.
Try SQL Server Data Tools for Visual studio :
https://msdn.microsoft.com/en-us/mt186501.aspx
Very easy and intuitive to use for Schema comparison and generating delta scripts.
I suggest you have a look at xSQL Schema Compare. It's a great tool for comparing and synchronizing database schemas. It should do the trick in your case. Also, this comes bundled with xSQL Data Compare which you can use to compare and synchronize the data as well, should the need to do so arise.
In my Application I have used Entity Framework Database First approach.
Currently my application is in Dev Environment, now it need to be moved into Test Environment and later into Production Environment.
So is there anyway that I can use .net feature or Entity framework feature to migrate/create database in Test environment. Other than using SQL feature of restoring the database.
Also note that if any enhancement comes then Database structure can change, table schema can change.
So can you suggest me the best way to easily migrate database schema in different environment without losing existing Data.
If you want to take advantage of EF-Migrations feature, you must convert your application to Code First with Existing Database http://msdn.microsoft.com/en-us/data/jj200620.aspx
If you are unable to convert to code first then you must create the update script by hand.
Use a schema compare tool, compare the development and production server.
For each difference found, create an update query.
Once the entire script is finished, test it on the staging server.
Automating the migration is very risky, it depends on the type and size changes you made to the schema. You can't trust any single feature or tool specially if the changes requires data motion (moving data around).
The following links might help you:
How to do Migrations in DB first approach
EF Migrations for Database-first approach?
With Database First, the easiest way to copy a schema is to extract a data tier application in management studio, create an empty database on the target, register it as a data tier application with the same name, and upgrade the empty database using the upgraded file. You can repeat this step to manage schema changes.
Having said that, going forward you're really better off switching your Database First to Code First as it will make change management across your deployments much easier.
Migrations are best way to deal with it
Preferred way to update production db is to first generate sql file and then run the sql file in production environment.
MS had a very good artical on this
http://msdn.microsoft.com/en-in/data/jj591621.aspx#script