Using Redgates Sql Compare for Complex Migrations with EF’s CodeFirst


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

LINQ Query Workaround for Comparing Dates (EF, LINQ2SQL,CodeFirst)


  I seem to always get this error all the time (kind of like I never learn).     Or, for you search engines reading: LINQ to Entities does not recognize the method 'System.DateTime Subtract(System.TimeSpan)' method, and this method cannot be translated into a store expression.   The solution is very straight forward.  Just pull out the Subtract and compare the dates directly like this:   DateTime compareDateTime = DateTime.UtcNow.Subtract(new TimeSpan(0, 0, 15, 0));// now get failures with last run more than 15 minutes agovar usersFailedLasttime =db.PushChannels.Where(a => a.RequestPushNotification.HasValue && a.RequestPushNotification.Value && … Continue Reading

Entity Framework Code First From Microsoft, Best and Worst Practices


  Introduction I’ve been building a fairly sophisticated product that will need to sustain a high transaction rate using the shipping version of Microsoft’s Entity Framework CodeFirst (4.1) which is part of Visual Studio.  I keep meaning to blog my experiences because I’ve been learning a huge amount about how to use and how to abuse the product.  My general style for building database intensive apps is not to spend a lot of time on optimization while I’m implementing, but to then go back and fix bottlenecks.  Back in the old days (last year), I would pretty much write my own views and then reference the with LINQ2SQL which pretty much solved most of my performance issues.  The reason is because views would allow … Continue Reading

Extending CodeFirst (EntityFramework) to Multiple Levels of 1 to 1 Relationships (Strongly Typed)


In my previous post, I showed how to take a simple relationship between Customers and an one Detail per customer build that into a relationship that eagerly loaded the customer record when retrieving a Detail record with CodeFirst in EntityFramework.  Now, let’s extend that so that a customer has an Address associated with them. This means, the relationship is as follows: Detail     Customer         Address Or, using my favorite Sql Manager Tool from EMS Database Management Solutions:     The model is now as follows: namespace Con1 { public class SiteDB : DbContext { public DbSet<Detail> Details { get; set; } … Continue Reading


Get every new post delivered to your Inbox

Join other followers: