Posts tagged .Net Framework

Entity Framework Seed AddOrUpdate Issue

, I’ve been using EF Code First since it was in CTP and hopped onto manual migrations bandwagon as soon as I could. I’m still on the bandwagon by the way and we use EF Code First and Manual Migrations to manage production (Microsoft Azure SQL), test and dev deployments of Resgrid. But this is not to say there aren’t issues, one of the big ones has been with EF’s Seed method.

entity_imageFor the uninitiated, EF has a method called Seed that’s part of the code configuration when your using EF Migrations (Automatic and Manual). This method allows EF to ensure the baseline required data is inside your database. For example type tables, test users\companies, etc. Every time EF starts up it runs through the Seed method to ensure your baseline data is correct. The recommend method for this is AddOrUpdate. Which allows you to set a unique key (or compound/complex key) and insert the record of it’s missing or update it if it’s changed (i.e. you fixed a spelling mistake in the type name field). Pretty cool huh?

Boy oh boy can that bite you in the ass. I’ve had seed records inserted thousands of times due to AddOrUpdate. The issue seems to arise when you use the following syntax:

context.Customers.AddOrUpdate(a => a.CustomerId,
    new Customer
    {
        CustomerId = 1,
        Name = "Test Customer",
        ShowWelcome = true,
        TimeZone = "Eastern Standard Time"
    });

CustomerId is a Primary Key, most of the time this will insert just fine. But then there are the fun times, where Customer won’t have an Id of 1, but instead 2, or 3 or 100. EF just gives up, for whatever reason and silently inserts a record that you didn’t want into the the table and can keep inserting it. This seems to happen when you have multiple inserts of the same record type.

In addition to having many records inserted you can get inserts completely out of order. Take for example this code.

context.Customers.AddOrUpdate(a => a.CustomerId,
    new Customer
    {
        CustomerId = 1,
        Name = "Test Customer",
        ShowWelcome = true,
        RefId = "50DEC5DB-2612-4D6A-97E3-2F04B7228C85",
        TimeZone = "Eastern Standard Time"
    },
    new Customer
    {
        CustomerId = 2,
        Name = "Test Customer 2",
        ShowWelcome = true,
        RefId = "AFA1E979-FCA9-417B-A03D-69C0588FAD71",
        TimeZone = "Pacific Standard Time"
    },
    new Customer
    {
        CustomerId = 3,
        Name = "Test Customer 3",
        ShowWelcome = true,
        RefId = "1AA0DCF8-4220-4C86-A10B-B6F8B3C0CA44",
        TimeZone = "Central Standard Time"
    }};

Based on it we would expect Test Customers to be record 1, Test Customer 2 to be record 2 and Test Customer 3 to be record 3. Would you believe me if I told you Test Customer 3 got CustomerId 1, Test Customer got CustomerId 2 and Test Customer 2 got CustomerId 3. WTF?!?!

Some solutions involve using a non-Primary Key field and instead focusing on another field in this example Name, or a combination like Name and Timezone (obviously I wouldn’t use Name and Timezone, you would want to use truly unique values). The syntax for a compound key looks like this:

context.Customers.AddOrUpdate(a => new { a.Name, a.SSN },
    new Customer
    {
        Name = "Test Customer",
        SSN = "555-55-5555",
        ShowWelcome = true,
        RefId = "50DEC5DB-2612-4D6A-97E3-2F04B7228C85",
        TimeZone = "Eastern Standard Time"
    }};

So the issue seems that before the end of the Seed method (for at least the mix up of the inserted objects) it re’orders them internal before they are sent to the server to be executed.

How did I get around this? I added context.SaveChanges(); in between each of the inserts that were causing issues.

context.Customers.AddOrUpdate(a => a.CustomerId,
    new Customer
    {
        CustomerId = 1,
        Name = "Test Customer",
        ShowWelcome = true,
        RefId = "50DEC5DB-2612-4D6A-97E3-2F04B7228C85",
        TimeZone = "Eastern Standard Time"
    }};
    
context.SaveChanges();    

context.Customers.AddOrUpdate(a => a.CustomerId,
    new Customer
    {
        CustomerId = 2,
        Name = "Test Customer 2",
        ShowWelcome = true,
        RefId = "AFA1E979-FCA9-417B-A03D-69C0588FAD71",
        TimeZone = "Pacific Standard Time"
    }};
    
context.SaveChanges();
    
context.Customers.AddOrUpdate(a => a.CustomerId,
    new Customer
    {
        CustomerId = 3,
        Name = "Test Customer 3",
        ShowWelcome = true,
        RefId = "1AA0DCF8-4220-4C86-A10B-B6F8B3C0CA44",
        TimeZone = "Central Standard Time"
    }};
    
context.SaveChanges();

Now my records are created properly. Somewhere in the chain of adding more objects to the database these referenced objects get re-ordered in the execution chain.

.Net’s Stagnation and Brain Drain

Recently Justin Angel posted an essay he’d written titled “The Collapse of the .Net Ecosystem, V3”. It’s an interesting read and very well could be flawed in many meaningful, statically and logical ways. But if you haven’t popped your head up in a while there are noticeably less .Net jobs then there were in 2006-2010. This could be relative or absolute, but when I was looking for a  job a few years ago the C#/.Net listing were few and far between.

brain-drain

The jobs that are out there aren’t at startups, they mostly aren’t working on solving interesting problems, they are Line of Business applications.  Head over to Angel.co and see how many of their 8,400+ startups are using C#, hint it’s less then 2.5%.

There is nothing wrong with Line of Business applications or enterprise app development. You can make a very good living doing this and get to work on some cool stuff, maybe. But without new people, new ideas, different points of view and fresh perspectives the .Net Community becomes an echo chamber. As a whole it will lag behind other more cutting edge development techniques and methodologies because we aren’t exposed to them, as people aren’t flocking to be .Net developers.

You may be saying to yourself, “That’s fine I don’t want to work for a startup anyways” and that’s a perfectly valid opinion. But it’s undeniable that startups attract the younger developers, who can take the risk and the commitment to the startup pace. These then become the next generation of enterprise developers. If there are only a handful of .Net startups what does that say about the possible new input into the .Net Development collective? How many developers are leaving? I’d bet you we have a net loss in people leaving the .Net community verses new minds coming in. As I sit here the path that is currently laid out for .Net developers is the track languages like COBOL followed.

For the last few years Stack Overflow has done a developer survey. You can head over to SO and check out the results. The section I want to focus on is “Most Popular Technologies”. Here you can easily compare 2013, 2014 and 2015 trends.

2015-06-15_8-02-02

Here is the breakdown for C# for the last 3 years:

2015 pharmacieviagra.com 2014 2013
C# 31.6% 37.6% 44.7%

In 2 years C#’s popularity on SO has dropped 13.1%. To be fair JavaScript dropped 4.5% as well from 2014, but they call out Node.js (13.3% and AngularJS 13.3%) and Angular didn’t have any numbers for the 2014 survey. The trend may not be alarming to all, but at a minimum it should let you take notice and stock of what the future looks like.

.Net will be around for a long time, but in 5 years how much new development will be done in it compared to how much maintenance? What about in 10? I love the C# language and the .Net Framework, it’s elegant, complete and very capable. But would I want to just fix bugs on a 10 year old legacy system day in and out with it? Sure you will be able to find C# work, but is it going to provide the challenges that keeps us developers engaged and provide job satisfaction?

I don’t blame Microsoft for this (ok maybe just a little), they are responding to what the market wants. You can’t continue to just push you language/framework as the only way to do things. Hell even Apple announced that they are open sourcing Swift. But times have changed, in the past you needed to do .Net for Windows development. Web application weren’t very good and to build any decent Windows Apps you used .Net. Eventually those Windows Apps got ported to ASP.Net, then ported to MVC. We went from Windows where really only C++ and .Net mattered to the Web where, well lots of things matter. Now to build a “Metro” app you can JavaScript or C++, no .Net required and there are tools to utilize “Metro” apps in the normal desktop.

I think with Microsoft Open Sourcing a good chunk of their ASP.Net stack they can compete better with Java, companies that don’t run Windows can use straight .Net now on their Linux/Unix boxes. Yes there was always Mono but that never really took off as I had hopped. So there is some light on the end of the end of the tunnel, hopefully it’s not a “No Exit” sign.

Resgrid is a SaaS product utilizing Microsoft Azure, providing logistics, management and communication tools to first responder organizations like volunteer fire departments, career fire departments, EMS, search and rescue, CERT, public safety, disaster relief organizations, etc. It was founded in late 2012 by myself and Jason Jarrett (staxmanade).

.Net & Xamarin Development on a Mac Part 2: Virtualization

Virtualization, who doesn’t like to talk about virtualization? No one I say. So you have 3 choices for virtualization when running on a Mac for your Windows VM’s, Parallels Desktop, VMWare Fusion and VirtualBox.

20120917_vmbench_teaserOver at TekRevue they did a great performance write-up comparing all three platforms and their comparative performance. Here I’m not really going to talk about outright performance, for that just go read that article, they did a far better job then I could.

Here is the TLDR, Parallels with a Windows VM on the Mac is a clear performance winner in startup times, graphics performance and ease of use.

 

But there is more to a virtualization platform then just pure performance. I’ve used both Parallels (versions 6 to 8) and VMWare Fusion 6 and 7. Besides emulation (i.e. the Xamarin Android Player and Genymotion) I haven’t, nor do I know anyone using VirtualBox for a mainline development VM. So I won’t be speaking about Virtual Box, it may be good but for our usage scenario there isn’t a lot of people doing it and thus when stuff goes wrong you can be SOL.

Pros Cons
Parallels Clear winner in perceived and actual performance More consumer grade/focus
Easy maintenance and optimization Lack of deep configuration options
Great integration with the Mac (Host OS) Lack of other OS support
VMWare Fusion Great support for a vast number of Operating Systems Slower performance and lower DirectX support
Deep configuration and tweaking options (i.e. Hyper-V) Not as frequently updated (Compared to Parallels)
Lots of Enterprise’y features (Migrations, Encryption, Sharing, etc) Sometimes complicated settings and options

So which one do I use? Currently I use VMWare Fusion 7. I made the switch to Fusion from Parallels during Parallels 8. During that time I felt that Fusion supported Retina displays better and it allowed for more configuration and tweaking of the virtual machine. I’m also a fan of how Fusion gives access to the VM through their menu. It’s far more compact and powerful then the corresponding Parallels one.

But you can’t go wrong with either choice in my opinion, they both work well. If you need the fastest performance you can muster or need the latest graphics support then Parallels is your best option, if you need rock solid stability and a deep field of configuration, tweaking and OS options then go Fusion.

In the context of what we are looking for virtualization for VMWare Fusion seems more popular. Xamarin uses it (all of the Xamarin University instructions have been on Mac with Fusion) and a fair amount of developers in the space seem to be following that same configuration. The benefit here is that the more people using that config the less problems there should be and the more help if an issue is encountered.

Resgrid is a SaaS product utilizing Microsoft Azure, providing logistics, management and communication tools to first responder organizations like volunteer fire departments, career fire departments, EMS, search and rescue, CERT, public safety, disaster relief organizations, etc. It was founded in late 2012 by myself and Jason Jarrett (staxmanade).

Go to Top