Friday, December 23, 2011

.NET Entity Framework: Detect if an Entity is new

When I'm using Entity Framework (EF) I'd like to have a Save() method that I can call without having to know what the state of the object is. I don't care if the object should be inserted or updated, that's up to the Save() method to find out.

In Entity Framework you can find out the state of an object by calling context.ObjectStateManager.GetObjectStateEntry(obj). To be able to use this method, your object needs to be attached to the context. If not, it will raise an InvalidOperationException. And that will be the case if you are saving an object that was just created (and not attached yet).

So what I do is using an extension method to determine what to do in the Save() method:

public static bool IsNew(this EntityObject entity)
{
  return entity.EntityKey == null || entity.EntityKey.IsTemporary;
}

If an object isn't attached yet, its EntityKey will be null. Otherwise, if it's attached but new, its EntityKey will be indicated as temporary.

The Save() mehtod calls the IsNew() method and acts accordingly.
private void Save(Product product)
{
  if (product.IsNew())
  {
    Insert(product);
  }
  else
  {
    Update(product);
  }
}

Thursday, September 15, 2011

Installing Windows 8 Developer Preview in VMware

Several versions of Windows 8 Developer Preview are public available on Microsoft’s Windows Developer Preview downloads. Just pick the one that fits for you. The downloaded file is an ISO file. You don’t need to burn it on disc.

If you don’t have a spare computer where you can install Windows 8 Developer Preview on, you can install it on your own machine using VMware. First you’ll need VMware Workstation 8 or VMware Player 4. The latter one is free, but at this moment you can only get it by downloading VMware Workstation 8. Don’t worry if you don’t have a license, you don’t need one because we will only use the free VMware Player 4. If you want full functionality you can download a trial version. For both downloads you’ll need to create an account. Go ahead and download and install VMware 8 if you have not already.

In this little tutorial I will use VMware Player, but the steps are nearly the same for VMware Workstation. Start VMware Player and click “Create a New Virtual Machine”. In the window that pops up, make sure you choose “I will install the operating system later.”

Installing-Win8-VMware-1

If you select one of the other options, you’ll get in trouble when Windows 8 Developer Preview is installing. Because in one of the next screens of the VMware wizard you’ll be asked to enter the license key for Windows 8 Developer Preview which you don’t have. You can leave it empty, but when Windows 8 Developer Preview is installing you’ll get the following error: “Windows cannot read the <ProductKey> setting from the unattend answer file”.

Installing-Win8-VMware-2

So, let’s proceed with the last option and click “Next >”. Depending on which version you have downloaded, select “Windows 7” or “Windows 7 x64” as operating system. I have downloaded the 64-bit version, so I chose “Windows 7 x64”.

Installing-Win8-VMware-3

Enter a Virtual machine name in the next step, for instance “Windows 8 Developer Preview”, and set the location of your virtual machine. I did not change anything in the next steps of the wizards, but you can choose the maximum disk size (default 60GB), whether to split the virtual disk or not. If you want to adjust the default allocated memory (1GB) you’ll need to click on “Customize Hardware…” in the last step to change it. This is my summary screen:

Installing-Win8-VMware-4

Click “Finish” and select the created virtual machine in VMware Player. Open the Virtual Machine Settings by clicking “Edit virtual machine settings” and select “CD/DVD IDE” in the device list. Make sure “Connect at power on” is checked and that you point it to the downloaded ISO file.

Installing-Win8-VMware-5

Click “OK” and power on the virtual machine. The Windows 8 Developer Preview installation wizard will start. At the end you can enjoy Windows 8 Developer Preview! One more tip: the metro style apps will only run if your screen resolution is at least 1024x768 (hardware or virtual machine).

UPDATE 1 (2 jan 2012): Some people have still trouble installing Windows 8 Developer Preview. You can fix that by removing the floppy of the virtual machine: Power off Windows 8 Virtual Machine, go to Virtual Machine Settings and remove the floppy drive. Thanks for the tip abkar, John and Claudiu!

UPDATE 2 (2 jan 2012): Modified links to download page for VMware Player.