C# EntityType';电影分类地图';没有定义键。定义此EntityType的键

C# EntityType';电影分类地图';没有定义键。定义此EntityType的键,c#,.net,entity-framework,entity-framework-5,C#,.net,Entity Framework,Entity Framework 5,请帮我解决这个问题。我已经坐了两天了,不知道哪里做错了。我在下面看到了这篇文章: 但我还没有解决我的问题,所以我终于问了这个问题。下面是我的完整课程,您可以复制该问题 电影实体 public class Movie { public Movie() { this.MovieCategoryList = new List<Movie_Category>(); } public string MovieID { get; se

请帮我解决这个问题。我已经坐了两天了,不知道哪里做错了。我在下面看到了这篇文章:

但我还没有解决我的问题,所以我终于问了这个问题。下面是我的完整课程,您可以复制该问题

电影
实体

public class Movie
{
    public Movie()
    {
        this.MovieCategoryList = new List<Movie_Category>();
    }
    public string MovieID { get; set; }
    public string MovieName { get; set; }
    public Nullable<int> YearReleased { get; set; }
    public virtual ICollection<Movie_Category> MovieCategoryList { get; set; }
}

public class MovieMap : EntityTypeConfiguration<Movie>
{
    public MovieMap()
    {
        // Primary Key
        this.HasKey(p => p.MovieID);

        // Property
        this.Property(p => p.MovieID)
            .IsRequired()
            .HasMaxLength(15);

        this.Property(p => p.MovieName)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(p => p.YearReleased)
            .IsOptional();

        // Table and Column Mappings
        this.ToTable("Movie");
        this.Property(p => p.MovieID).HasColumnName("MovieID");
        this.Property(p => p.MovieName).HasColumnName("MovieName");
        this.Property(p => p.YearReleased).HasColumnName("YearReleased");

        // Relationships
    }
public class Category
{
    public Category()
    {
        this.MovieCategoryList = new List<Movie_Category>();
    }
    public string CategoryID { get; set; }
    public string CategoryName { get; set; }
    public virtual ICollection<Movie_Category> MovieCategoryList { get; set; }
}

public class CategoryMap : EntityTypeConfiguration<Category>
{
    public CategoryMap()
    {
        // Primary Key
        this.HasKey(p => p.CategoryID);

        // Property
        this.Property(p => p.CategoryID)
            .IsRequired()
            .HasMaxLength(15);

        this.Property(p => p.CategoryName)
            .IsRequired()
            .HasMaxLength(30);

        // Mappings
        this.ToTable("Category");
        this.Property(p => p.CategoryID).HasColumnName("CategoryID");
        this.Property(p => p.CategoryName).HasColumnName("CategoryName");

    }
}
以及
Movie\u Category
,它将
Movie
映射到
Category
,因为它是一种
多对多
关系

public class Movie_Category
{
    public int MovieCategoryID { get; set; }
    public string MovieID { get; set; }
    public string CategoryID { get; set; }

    public virtual Movie Movie { get; set; }
    public virtual Category Category { get; set; }
}

public class Movie_Category_Map : EntityTypeConfiguration<Movie_Category>
{
    public Movie_Category_Map()
    {
        // Primary Key
        this.HasKey(p => p.MovieCategoryID);

        // Property
        this.Property(p => p.MovieCategoryID)
            .IsRequired()
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.Property(p => p.CategoryID)
            .IsRequired()
            .HasMaxLength(15);

        this.Property(p => p.MovieID)
            .IsRequired()
            .HasMaxLength(15);

        // Mappings
        this.ToTable("Movie_Category");
        this.Property(p => p.MovieCategoryID).HasColumnName("RecordID");
        this.Property(p => p.MovieID).HasColumnName("MovieID");
        this.Property(p => p.CategoryID).HasColumnName("CategoryID");

        // Relationship
        this.HasRequired(t => t.Category)
            .WithMany(t => t.MovieCategoryList)
            .HasForeignKey(p => p.CategoryID);

        this.HasRequired(t => t.Movie)
            .WithMany(t => t.MovieCategoryList)
            .HasForeignKey(p => p.MovieID);

    }
}
因此,在我的
数据访问层
中,我有以下代码:我已经删除了try-catch块

当我试图运行代码时,它会停止运行

_db.MovieList.Add(_movie);
并抛出此异常消息

在模型生成过程中检测到一个或多个验证错误:

\tSystem.Data.Entity.Edm.EdmEntityType::EntityType “电影类别映射”未定义键。为此定义密钥 EntityType

\tSystem.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet “MovieCategory”基于没有键的类型“Movie\u Category\u Map” 定义

据我所知,在
Movie\u Category\u Map
配置中,我已经设置了一个主键

this.HasKey(p => p.MovieCategoryID);
你能告诉我哪里做错了吗?或者我在哪里丢失了?

公共DbSet MovieCategory{get;set;}
public DbSet<Movie_Category_Map> MovieCategory { get; set; }
这条线给你添麻烦了

这应该是

public DbSet<Movie_Category> MovieCategory { get; set; }
public DbSet MovieCategory{get;set;}
您正在将EntityTypeConfiguration添加为一个实体,因此它正在呼喊密钥(当然,密钥不存在)

public DbSet<Movie_Category_Map> MovieCategory { get; set; }
public DbSet<Movie_Category> MovieCategory { get; set; }