2

请帮我解决这个问题。我已经坐了 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_Categorywhich 映射Movie到,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' 没有定义键。定义此 EntityType 的键。

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

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

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

你能指出我哪里做错了吗?或者我在哪里失踪?

4

1 回答 1

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

这条线给你带来麻烦

这应该是

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

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

于 2012-12-24T06:40:41.053 回答