实体框架迁移失败

时间:2021-05-25 16:25:29

标签: c# entity-framework .net-core entity-framework-6 relational-database

我正在开发一个使用 EF 的 DotNet Core 应用程序。我有一个正在运行的数据库,我正在重新配置我的模型类,以便它们彼此之间具有正确的关系。我有 4 个表:电影、用户、评论和流派。 Genre 和 Movie 应该是多对多的关系,Movie 和 Review 必须是一对多的,User 必须和 Review 是一对多的关系。我已将模型配置如下:

public class Movie
    {
        [Key]
        public int MovieId { get; set; }
        [Required]
        public string imdbId { get; set; }
        public string title { get; set; }
        public virtual ICollection<Genre> Genres { get; set; }
    }

public class User
    {
        public int UserId { get; set; }
        public string Username { get; set; }
    }

public class Review
    {
        [Key]
        public int ReviewId { get; set; }

        public int goreRating { get; set; }
        public int shockRating { get; set; }
        public int jumpRating { get; set; }
        public int plotRating { get; set; }
        public int supernaturalRating { get; set; }
        public int starRating { get; set; }
        public int MovieId { get; set; }
        public User User { get; set; }
        public virtual Movie Movie { get; set; }    
    }

public class Genre
    {
        public int GenreId { get; set; }
        //Navigational Properties
        public virtual ICollection<Movie> Movies { get; set; }
    }

在迁移后尝试更新数据库时,我收到错误'数据库中已经有一个名为'流派'的对象',并且:

Applying migration '20210514094730_initial'.
Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Genres] (
    [Id] int NOT NULL IDENTITY,
    CONSTRAINT [PK_Genres] PRIMARY KEY ([Id])
);

我尝试删除电影模型中的导航属性,因为我认为这会引发错误。但是,当 EF 尝试应用初始迁移并且无法创建 Genre 时会发生错误,因为这是在初始迁移中创建的第一个表。这很奇怪,因为在此之前我已经成功添加了 12 个迁移(和更新)。我一直无法在网上找到类似的问题。为什么我收到这个错误?我的导航属性是否配置不正确?

1 个答案:

答案 0 :(得分:0)

您没有正确配置多对多关系。最好是制作另一个具有流派和电影表外键的中间模型,并且这两个表需要具有该中间表的 ICollection 作为属性。

而且对于Movie 和Review 之间的一对多关系,您需要Movie 表中的Review 集合。

public class Movie
{
    [Key]
    public int MovieId { get; set; }
    [Required]
    public string imdbId { get; set; }
    public string title { get; set; }
    public ICollection<MovieGenre> MovieGenres { get; set; }
    public ICollection<Review> Reviews { get; set; }
}



public class Genre
{
  [Key] 
  public int GenreId { get; set; }
  public ICollection<MovieGenre> MovieGenres { get; set; }
}


public class MovieGenre
{
   public int MovieId{ get; set; }
   public Movie Movie{ get; set; }
   public int GenreId{ get; set; }
   public Genre Genre{ get; set; }
}

用于多对多关系的另一件事是 DbContext 类中的 FluentApi 以成功实现关系。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<MovieGenre>()
        .HasKey(bc => new { bc.Movie, bc.Genre });  

//for relation between movie and moviegenre

    modelBuilder.Entity<MovieGenre>()
        .HasOne(bc => bc.Movie)
        .WithMany(b => b.MovieGenres)
        .HasForeignKey(bc => bc.MovieId);  

//for relation between genre and moviegenre

    modelBuilder.Entity<MovieGenre>()
        .HasOne(bc => bc.Genre)
        .WithMany(c => c.MovieGenres)
        .HasForeignKey(bc => bc.GenreId);
}