Introduction As many of you know, I’ve been quite the fan of Entity Framework CodeFirst for quite some time. It does a great job of reducing the number of places I have to change thngs each time I have a database schema change and also brings a ton of type safety along with it. I’ve posted before about the benefits of the auto migrations features built into CodeFirst, however I have found when I make a lot of changes to my schema, I quickly get an error saying I will have data loss so the CodeFirst Migration will not take place. In my realities, it is just too much time to go through with LINQ and try and make the perfect data migration. My reality is that I likely only have one or two databases that need sql updates and … Continue Reading
When Using EF CodeFirst With Visual Studio, How to Live With Cannot drop database because it is in use error.
Problem So, if you have been doing development with Visual Studio 2010, Entity Framework CodeFirst, SqlServer or SqlServerCE for any amount of time, you’ll quickly run into the problem that the database can not be reinitialized because it is open. Basically, the scenario is this. 1) Put in your Global.asax.cs file a line that always recreates the database (naturally because you are in a development mode and as you constantly change your model and seed data). The line is something like this: Database.SetInitializer(new DropCreateDatabaseAlways<SiteDB>()); 2) Run your application with something like Debug/Run (All is fine) 3) Go into the database browser (either in Visual Studio or Enterprise … Continue Reading
In this post, we will show and explain a small TSQL Sql Server 2008 procedure that deletes all rows in a table that are older than some specified date. That is, say the table has 10,000,000 rows in it the accumulated over the past 2 years. Say you want to delete all but the last 30 days of activity. If you just simply say DELETE FROM table WHERE id>10000, you will cause this to happen in one transaction and likely, you will get an error. That’s the best case. The worst case is your system tries to do this, eventually consumes all the resources in your computer and crashes your server. … Continue Reading