Definition of the relationship of many to many in the first structure of the essence of the code

I have 3 classes in my model, as you can see below.

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string UserName { get; set; }

    public ICollection<MartialArtUserProfile> MartialArtUserProfiles { get; set; }
}

[Table("MartialArt")]
public class MartialArt
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public string IconPath { get; set; }

    public string ImagePath { get; set; }

    public ICollection<MartialArtUserProfile> MartialArtUserProfiles { get; set; }
}

public class MartialArtUserProfile
{
    public int UserProfileId { get; set; }
    public UserProfile UserProfile { get; set; }

    public int MartialArtId { get; set; }
    public MartialArt MartialArt { get; set; }
}

And I have a configuration class for many, many relationships, as shown below:

public class MartialArtUserProfileConfiguration : EntityTypeConfiguration<MartialArtUserProfile>
{
    public MartialArtUserProfileConfiguration()
    {
        HasKey(a => new { a.MartialArtId, a.UserProfileId });

        HasRequired(a => a.MartialArt)
            .WithMany(s => s.MartialArtUserProfiles)
            .HasForeignKey(a => a.MartialArtId)
            .WillCascadeOnDelete(false);

        HasRequired(a => a.UserProfile)
            .WithMany(p => p.MartialArtUserProfiles)
            .HasForeignKey(a => a.UserProfileId)
            .WillCascadeOnDelete(false);
    }
}

After defining my entities, when I try to run Update-Database in the Package Manager Console , it says:

During model generation, one or more validation errors were detected:

\ tSystem.Data.Entity.Edm.EdmEntityType :: EntityType 'MartialArtUserProfile' does not have a key. Define a key for this EntityType. \ tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'MartialArtUserProfiles' is based on the type 'MartialArtUserProfile', which does not have specific keys.

?

,

+5
3

, . , . Fluent API , . UserProfileToMartialArt , , . MartialArtUserProfile EF . , .

modelBuilder.Entity<UserProfile>()
    .HasMany(b => b.MartialArts)
    .WithMany(a => a.UserProfiles)
    .Map(m => m.MapLeftKey("MartialArtId")
        .MapRightKey("UserProfileId")
        .ToTable("UserProfileToMartialArt"));

MartialArts

public IList<UserProfile> UserProfiles { get; set; }   

UserProfile

public IList<MartialArt> MartialArts { get; set; }  
+8

:

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string UserName { get; set; }

    [InverseProperty("UserProfiles")]
    public IList<MartialArt> MartialArts { get; set; }
}

[Table("MartialArt")]
public class MartialArt
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public string IconPath { get; set; }

    public string ImagePath { get; set; }

    [InverseProperty("MartialArts")]
    public IList<UserProfile> UserProfiles { get; set; }
}
+6

EntityFramework 6.1 - , .

public class UserProfile {
    public int Id { get; set; }
    public string UserName { get; set; }
    public virtual ICollection<MartialArt> MartialArts { get; set; }

    public UserProfile() {
        MartialArts = new List<MartialArt>();
    }
}

public class MartialArt {
    public int Id { get; set; }
    public string Name { get; set; }
    // *snip*
    public virtual ICollection<UserProfile> UserProfiles { get; set; }

    public MartialArt() {
        UserProfiles = new List<UserProfile>();
    }
}
+5

All Articles