Error Adding View in June CTP: CS0104 ColumnAttribute is an Ambiguous Reference


Recently I upgraded to the Entity Framework June 2011 CTP to be able to use Enums in my project model.  All went well.  Until I needed to add a strongly typed view that wasn’t empty.  When trying to create the view I received two errors:

  • CS0104 - Compiling transformation: 'ColumnAttribute' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.ColumnAttribute' and  System.Data.Linq.Mapping.ColumnAttribute’
  • CS1061 – Compiling transformation:  'System.ComponentModel.DataAnnotations.ColumnAttribute' does not contain a definition for 'IsPrimaryKey' and no extension method 'IsPrimaryKey' accepting a first argument of type 'System.ComponentModel.DataAnnotations.ColumnAttribute' could be found…

 

Add View Error

The first error occurs because ColumnAttribute is in both System.ComponentModel.DataAnnotations and System.Data.Linq.Mapping.  It is unclear (ambiguous) as to which should be used.  I believe that the second error results from the an attempt to use System.ComponentModel.DataAnnotations to create the view which does not contain a definition for IsPrimaryKey.  So somehow we need to get Visual Studio to use System.Data.Linq.Mapping instead.  After a bit of Googling the following solution presented itself:

Navigate to C:\<Visual Studio Install Path>\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\AddView\CSHTML

Here you will see all the .tt files for Create, Delete, Details, Edit,Empty and List.

You will need to edit all of these files except for Empty.tt

Find the line:

var column = attribute as ColumnAttribute;

Find Line

Change it to:

var column = attribute as System.Data.Linq.Mapping.ColumnAttribute;

Line Edit

Save and restart Visual Studio.  Try and add a view!

6 Comments

Add a Comment