Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 482
Ruslan Avatar asked Feb 18 '19 20:02

Ruslan


People also ask

How do you set composite primary key in fluent API?

The only way to configure composite keys is to use the HasKey method. You specify the properties that form the composite key by passing them in as properties of an anonymous type to the HasKey method.


2 Answers

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; }
}
like image 148
Leonel Sanches da Silva Avatar answered Oct 04 '22 20:10

Leonel Sanches da Silva


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.

like image 40
Chris Pratt Avatar answered Oct 04 '22 20:10

Chris Pratt