EF 4.1 Code First: each property name in a type must be a unique error in the Lookup Table association

This is my first attempt to create my own EF model, and I found myself trying to create a lookup table association using Code First, so I can access:

myProduct.Category.AltCategoryID

I have installation models and mappings, as I understand it, right, but keep getting    error 0019: each property name in the type must be unique. Property name 'CategoryID' already defined

The following models are represented in my code:

[Table("Product", Schema="mySchema")]
public class Product {
    [Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None)]
    public int ProductID { get; set; }
    public int CategoryID { get; set; }
    public virtual Category Category { get; set; }
}

[Table("Category", Schema="mySchema")]
public class Category {
    [Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None)]
    public int CategoryID { get; set; }
    public string Name { get; set; }
    public int AltCategoryID { get; set; }
}

I have specified associations with:

modelBuilder.Entity<Product>()
                    .HasOptional(p => p.Category)
                    .WithRequired()
                    .Map(m => m.MapKey("CategoryID"));

I tried several other things, including adding the [ForeignKey] annotation, but this results in an error containing a link to the ProductID field.

+5
1

:

modelBuilder.Entity<Product>()
            // Product must have category (CategoryId is not nullable)
            .HasRequired(p => p.Category)     
            // Category can have many products  
            .WithMany()                       
            // Product exposes FK to category  
            .HasForeignKey(p => p.CategoryID);
+8

All Articles