Entity Framework Code First “Sequence contains more than one element”

Problem

When you run “update-database” in Package Manager Console, you see error “Sequence contains more than one element”.

Troubleshoot

The 1st step is to look the exception stack trace until you can locate the line of code that is causing the error:

   at My.Entity.Migrations.Configuration.Seed(MyContext context) in e:\MyApp\My.Entity\Migrations\Configuration.cs:line 89

So you can view what entity it is trying to AddOrUpdate.  Verify the following 3 things:

  1. You are specifying a unique key or a unique complex key.
  2. The objects you are trying to insert do not violate that uniqueness.
  3. The existing data in the database table do not violate that uniqueness.

To specify a unique complex key, use new { column1, column2, …}

For example,

context.AppSettings.AddOrUpdate(p => new { p.RunningEnv, p.AppMachineName, p.AppName,  p.AppKey },
new AppSetting { RunningEnv = "Dev", AppName = "App", AppMachineName = "dev2", AppKey = "MyName", AppValue = "xxxx", LastModified = DateTime.UtcNow }
);

Advertisements

Downgrade from Windows 8 to Windows 7

I have decided to downgrade from windows 8 to windows 7 before I hand over a new laptop to a developer, for familiarity and efficiency sake.

It turns out there are a few hurdles: 

  • The Toshiba Satellite laptop does not support SSD drive, or at least the Toshiba rep claimed.  So much for the upgrade.
  • Windows 7 Installation Disk will freeze on boot up… when the 4 windows part connects.  I had to tweak the BIOS to use CSM Boot instead of UEFI Boot.  Other people claim that Windows 7 64x can boot with UEFI.
  • The installed HDD has a GPT partition table.  Win 7 cannot install to it.  I had to use GParted on a USB bootable flash drive to convert the disk to MBR (msdos).

References:

http://gparted.org/liveusb.php

http://superuser.com/questions/642420/tools-to-convert-gpt-to-mbr-without-access-to-an-os

How to Eject CD/DVD When Files In Use

Problem

There comes a time when you have a CD/DVD drive with no eject button nor emergency pinhole to poke (think Mac Mini).

And you are in Windows and you keep right click on the drive and try to eject via windows explorer.  However, it keeps telling you that files are in use and you must close the programs using the files first.

Solution

  1. Start –> Computer –> Right Click –> Manage to launch Computer Management
  2. Under Computer Management (Local), expand Storage, and click Disk Management
  3. In the center panel bottom section, look for your CD-ROM drive, right click and select Eject.

How to Fix a Slow WordPress Site on Windows 2012 IIS 8

While there are many ways to tune a WordPress installation on IIS, I have 4 performance tips:

  1. Install WordPress via Web Platform Installer (aka WebPI) 2.0 / Windows Web App Gallery.  http://www.microsoft.com/web/gallery/
    1. image
  2. Install/Activate/Configure W3 Total Cache plugin for WordPress
  3. Modify wp-config.php. 
    • Change define(‘DB_HOST’, ‘localhost’); to define(‘DB_HOST’, ’127.0.0.1′);
  4. Leverage browser caching by adding the <staticContent> to web.config.
<system.webServer>
...
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
</system.webServer>

 

Reference:

http://www.appliedi.net/blog/10-ways-to-improve-wordpress-performance-on-windows/

http://www.andrewdenhertog.com/web/solved-wordpress-slow-on-iis/

Windows Scheduled Task Failed with Error Value: 2147942402

This stumped me for an hour when I deployed a new version of the application to be called by an existing scheduled task.  However, the task kept failing to launch, with a ResultCode 2147942402.

Google search didn’t yield anything useful.

It turns out the error is caused by the simple fact that the application has changed and it needs to be updated in the scheduled task’s Action properties.

So the lesson learned here is to try recreate a scheduled task when you get this error.