简体   繁体   中英

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

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.

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; }
}

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 });      
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM