gpt4 book ai didi

c# - EntityType 'Movie_Category_Map' 没有定义键。为此 EntityType 定义键

转载 作者:行者123 更新时间:2023-11-30 22:22:03 26 4
gpt4 key购买 nike

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

但我还没有解决我的问题,所以我终于问了这个问题。以下是我的完整类(class),因此您可以重现该问题。

电影实体

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_CategoryMovie 映射到 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);

}
}

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 block

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

并抛出这个异常信息,

One or more validation errors were detected during model generation:

\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Movie_Category_Map' has no key defined. Define the key for this EntityType.

\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'MovieCategory' is based on type 'Movie_Category_Map' that has no keys defined.

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

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

你能指出我哪里做错了吗?还是我哪里不见了?

最佳答案

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

这条线给你带来麻烦

应该是

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

您正在添加 EntityTypeConfiguration 作为一个实体,所以它正在呼唤 key (当然不存在)

关于c# - EntityType 'Movie_Category_Map' 没有定义键。为此 EntityType 定义键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14017898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com