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.