请帮我解决这个问题。我已经坐了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_Category
映射到Movie
的{{1}},因为它是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());
}
}
中,我有这个代码:我删除了try-catch块
Data Access Layer
当我尝试运行代码时,它会在线停止
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);
}
并抛出此异常消息,
在模型生成期间检测到一个或多个验证错误:
\ tSystem.Data.Entity.Edm.EdmEntityType :: EntityType 'Movie_Category_Map'没有定义键。为此定义密钥 的EntityType。
\ tSystem.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet 'MovieCategory'基于没有键的'Movie_Category_Map'类型 定义
据我所知,在_db.MovieList.Add(_movie);
配置中,我已经设置了主键,
Movie_Category_Map
你能指出我哪里做错了吗?或者我在哪里错过了?
答案 0 :(得分:3)
public DbSet<Movie_Category_Map> MovieCategory { get; set; }
这一行给你带来了麻烦
这应该是
public DbSet<Movie_Category> MovieCategory { get; set; }
您正在将EntityTypeConfiguration添加为实体,因此它正在为密钥(当前不存在密钥)进行呼叫