简体   繁体   中英

Entity Framework is Failing Migrations

I am working on a DotNet Core application that uses EF. I have a database working, and I am reconfiguring my model classes so they have the correct relations with one-another. I have 4 tables: Movie, User, Review and Genre. Genre and Movie should have a many-to-many relationship, Movie and Review must be one-to-many, and User must have a one-to-many relationship with Review. I have configured the models as follows:

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

When trying to update-database following a migration, I get the error 'There is already an object named 'Genres' in the database' and this:

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

I have tried removing the navigation property in Movie model, as I thought this was throwing the error. However, the error occurs as EF is trying to apply the initial migration and it's failing to create Genre as this is the first table created in the initial migration. It's strange because I've successfully added maybe 12 migrations (and updated) prior to this. I have been unable to find a similar issue online. Why am I getting this error? Are my navigation properties incorrectly configured?

You have not configured Many to Many Relationship correctly. It is better achieved by making another intermediate model that has foreign keys of both Genre and Movie table, and these two table needs to have ICollection for that intermediate table as a property.

and also for one to many relationship between Movie and Review , you need a collection of Review in Movie table.

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

Another thing to use for many to many relationship is FluentApi in DbContext Class to achieve the relationship successfully.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM