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
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.
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; }
}
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With