【问题标题】:EntityType 'Movie_Category_Map' has no key defined. Define the key for this EntityTypeEntityType 'Movie_Category_Map' 没有定义键。定义此 EntityType 的键
【发布时间】:2012-12-10 16:08:08
【问题描述】:

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

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

Movie实体

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
    }

Category实体

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 映射到CategoryMovie_Category,因为它是Many-to-Many 关系

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

    }
}

MovieShopContext 继承自 DbContext

public class MovieShopContext : DbContext
{
    public DbSet<Movie> MovieList { get; set; }
    public DbSet<Category> CategoryList { get; set; }
    public DbSet<Movie_Category_Map> MovieCategory { get; set; }

    static MovieShopContext()
    {
        Database.SetInitializer<MovieShopContext>(null);
    }

    public MovieShopContext()
        : base(@"Data Source=Newbie;Initial Catalog=SampleDB;Integrated Security=True;")
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Configurations.Add(new MovieMap());
        modelBuilder.Configurations.Add(new CategoryMap());
        modelBuilder.Configurations.Add(new Movie_Category_Map());
    }
}

所以在我的Data Access Layer 中,我有这个代码:我已经删除了 try-catch 块

using (MovieShopContext _db = new MovieShopContext())
{
    Movie _movie = new Movie();
    _movie.MovieID = "M-1212-001";
    _movie.MovieName = "Book of Riddles";
    _movie.YearReleased = 2001;

    _db.MovieList.Add(_movie);  // stops here

    _db.SaveChanges();
    MessageBox.Show("Action Completed!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

当我尝试运行代码时,它在线停止

_db.MovieList.Add(_movie);

并抛出此异常消息,

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

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Movie_Category_Map' 没有定义键。为此定义密钥 实体类型。

\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet “MovieCategory”基于没有键的“Movie_Category_Map”类型 已定义。

据我所知,在Movie_Category_Map配置中,我已经设置了主键,

this.HasKey(p => p.MovieCategoryID);

你能指出我哪里做错了吗?还是我错过了什么?

【问题讨论】:

    标签: c# .net entity-framework entity-framework-5


    【解决方案1】:
    public DbSet<Movie_Category_Map> MovieCategory { get; set; }
    

    这条线给你带来了麻烦

    这应该是

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

    您正在将 EntityTypeConfiguration 添加为实体,因此它正在为 Key 大喊(当然不存在)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-02
      • 2016-03-16
      • 2013-08-04
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多