'Entity type ‘Business_attrib2object’ has composite primary key defined with data annotations. To set composite primary key, use fluent API

I set composite primary key using fluent API, it is still an error, when I'm trying to create ClassesController (MVC Controller with Views Using Entity Framework).

Declaring Classes class:

public partial class Classes
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Classes()
    {
        this.Business_attrib2object = new HashSet<Business_attrib2object>();
        this.Objects = new HashSet<Objects>();
    }
    [Key]
    public System.Guid IdClass { get; set; }
    public string Name { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Business_attrib2object> Business_attrib2object { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Objects> Objects { get; set; }
}

Declaring Business_attrib2object class:

 public partial class Business_attrib2object
{
    [Key]
    public System.Guid IdClass { get; set; }
    [Key]
    public System.Guid IdAttribute { get; set; }

    public Nullable<System.Guid> IdAuthor { get; set; }

    public virtual Attributes Attributes { get; set; }
    public virtual Classes Classes { get; set; }
}

DBContext:

public class Business_attrib2objectContext : DbContext
{
    public DbSet<Business_attrib2object> Business_attrib2object { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Business_attrib2object>().HasKey(ba => new { ba.IdClass, ba.IdAttribute });      
    }

    public Business_attrib2objectContext(DbContextOptions<Business_attrib2objectContext> options)
        : base(options)
    {
        Database.EnsureCreated();
    }
}

Creating controller: Creating controller Error: Error message



Solution 1:[1]

Not sure what the confusion is. The error is explicit. You've got data attributes specifying a composite primary key, and you can't do that. You've already got the necessary fluent config in your context, so just remove the two [Key] data attributes on your Business_attrib2object class.

Solution 2:[2]

If you used this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Business_attrib2object>().HasKey(ba => new { ba.IdClass, ba.IdAttribute });      
}

You don't need this:

public partial class Business_attrib2object
{
    [Key]
    public System.Guid IdClass { get; set; }
    [Key]
    public System.Guid IdAttribute { get; set; }

    ...

Just remove the [Key] attributes that it should work:

public partial class Business_attrib2object
{
    public System.Guid IdClass { get; set; }
    public System.Guid IdAttribute { get; set; }

    public Nullable<System.Guid> IdAuthor { get; set; }

    public virtual Attributes Attributes { get; set; }
    public virtual Classes Classes { get; set; }
}

Solution 3:[3]

Answer is incomplete (at least for EF6):

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Business_attrib2object>().HasKey(ba => new { 
    ba.IdClass, ba.IdAttribute });      
}

Use this instead:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<Business_attrib2object>().HasKey(ba => new { 
    ba.IdClass, ba.IdAttribute });      
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris Pratt
Solution 2 Leonel Sanches da Silva
Solution 3 Anthony Padgett