0

I have a Movie model and a Genre class as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace web_template_001.Models
{
    public class Movie
    {
        [Required]
        public int Id { get; set; }
        [Required]
        [StringLength(25)]
        public string Title { get; set; }
        [Required]
        public Genre Genre { get; set; }
        public byte GenreId { get; set; }
        public DateTime DateAdded { get; set; }
        public DateTime ReleaseDate { get; set; }
        public byte NumberInStock { get; set; }
    }

    public class Genre
    {
        [Required]
        public byte Id { get; set; }
        [Required]
        [StringLength(25)]
        public string Name { get; set; }
    }
}

When trying to use Update-Database after creating the migration shown here:

namespace web_template_001.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class ModifiedMovieProperties : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Genres",
                c => new
                    {
                        Id = c.Byte(nullable: false),
                        Name = c.String(nullable: false, maxLength: 25),
                    })
                .PrimaryKey(t => t.Id);
            
            AddColumn("dbo.Movies", "GenreId", c => c.Byte(nullable: false));
            AddColumn("dbo.Movies", "DateAdded", c => c.DateTime(nullable: false));
            AddColumn("dbo.Movies", "ReleaseDate", c => c.DateTime(nullable: false));
            AddColumn("dbo.Movies", "NumberInStock", c => c.Byte(nullable: false));
            CreateIndex("dbo.Movies", "GenreId");
            AddForeignKey("dbo.Movies", "GenreId", "dbo.Genres", "Id", cascadeDelete: true);
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.Movies", "GenreId", "dbo.Genres");
            DropIndex("dbo.Movies", new[] { "GenreId" });
            DropColumn("dbo.Movies", "NumberInStock");
            DropColumn("dbo.Movies", "ReleaseDate");
            DropColumn("dbo.Movies", "DateAdded");
            DropColumn("dbo.Movies", "GenreId");
            DropTable("dbo.Genres");
        }
    }
}

I get this error:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Movies_dbo.Genres_GenreId". The conflict occurred in database "aspnet-web_template_001-20230102051540", table "dbo.Genres", column 'Id'.

Here is the git repo: https://github.com/joshuamitchum89/web_template_001

How do I resolve this issue?

Thank you for taking time to help me.

I have tried to remove the migration and make a new one. Also restarted VS to no end. I have used this approach for other models and classes to link table via foreign keys.

1 Answer 1

0

You need to add the [Key] attribute to the Id properties to both types. See below.

Also, you're key on Gene is a byte, not sure if this is an accident or intentional, but it seems like the former.

 [Key] //fix on both classes
 public int Id { get; set; } 

 //optional if you want to be able to access key
 public int Gene_Id { get; set; }

 [ForeignKey(nameof(Gene_Id))]
 public Genre Genre { get; set; }
Sign up to request clarification or add additional context in comments.

2 Comments

I added both [Key] attributes to each class as you stated and the error did not resolve.
If you can re-link the code, I'll take a look. It's hard to tell without seeing what in the migration that created Movie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.