Saturday, 22 September 2012

Visual Studio 2012 Express error: MSTestAdapter could not discover the test

I have upgraded a Visual Studio 2010 solution targeting .Net 4 to Visual Studio 2012 - Express Web.

For some reason I cannot run my tests and I get the following error:
MSTestAdapter could not discover the test because the classic mode helper is not available. If a TestSettings file has been selected, unselect it and try again.

UPDATED:

My VS 2010 solution contained the following files:
- Local.testsettings
- SolutionName.vsmdi
- TraceAndTestImpact.testsettings

After upgrading the project to VS 2012 the files above were kept as part of the solution.

To fix the issue:
- Remove all 3 files from the solution
- Delete all 3 files from the solution folder
- Close the solution in VS12 and reopen it

Sunday, 2 September 2012

Entity Framework 5: auto generated GUID as primary key

If you are using Entity Framework 5 - Code First and want to create an auto generated GUID as primary key, you have two options.

Consider the following POCO class:
public class Album
{
    public Guid ID { get; set; }
    public DateTime DateCreated { get; set; }
}
1) Data Annotations
Decorate your Primary Key with DatabaseGenerated / DatabaseGenerationOption.Identity.

[DatabaseGenerated(DatabaseGenerationOption.Identity)]
public Guid ID { get; set; }
Note: you can also use the "Key" attribute, but since the name of my property is ID Entity Framework will automatically detect that this field is a primary key.

2) Fluent API
In your DbContext class, override the OnModelCreating method and add the following line:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Album>().Property(p => p.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}