when I was working on the e-tickets project, I was trying to use "add migration Initial", but I'm encountering this error (The relationship from 'Movie.Cinema' to 'Cinema.Movies' with foreign key properties {'CinemaId' : int} cannot target the primary key {'Logo' : string} because it is not compatible. Configure a principal key or a set of foreign key properties with compatible types for this relationship.) However, since all the properties such as CinemaId seemed the same based on the tutorial I watched, I'm not sure how to resolve it. Here are my codes:
namespace eTicekts.Data
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext>options):base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Actor_Movie>().HasKey(am => new
{
am.ActorId,
am.MovieId
});
modelBuilder.Entity<Actor_Movie>().HasOne(m=>m.movie).WithMany(am=>am.actor_Movies).HasForeignKey(am=>am.MovieId);
modelBuilder.Entity<Actor_Movie>().HasOne(m => m.actor).WithMany(am => am.actor_Movies).HasForeignKey(am => am.ActorId);
base.OnModelCreating(modelBuilder);
}
public DbSet<Actor> Actors { get; set; }
public DbSet<Movie> Movies { get; set; }
public DbSet<Actor_Movie> Actor_Movies { get; set; }
public DbSet<Cinema> Cinmeas { get; set; }
public DbSet<Producer> producers{ get; set; }
}
}
namespace eTicekts.Models
{
public class Actor_Movie
{
public int MovieId { get; set; }
public Movie movie { get; set; }
public int ActorId { get; set; }
public Actor actor { get; set; }
}
}
namespace eTicekts.Models
{
public class Actor
{
[Key]
public int Id { get; set; }
public string ProfilePictureURL { get; set; }
public string FullName { get; set; }
public string Bio { get; set; }
//relationships
public List<Actor_Movie> actor_Movies { get; set; }
}
}
namespace eTicekts.Models
{
public class Cinema
{
[Key]
public string Logo { get; set; }
public string Name { get; }
public string Description { get; }
//relationships
public List<Movie> Movies { get; set; }
}
}
{
public class ErrorViewModel
{
public string? RequestId { get; set; }
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
namespace eTicekts.Models
{
public class Movie
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int Price { get; set; }
public int ImageURL { get; set; }
public DateTime StartDate { get; set;}
public DateTime EndDate { get; set;}
public MovieCategory MovieCategory { get; set; }
//relationships
public List<Actor_Movie> actor_Movies { get; set; }
//cinema
public int CinemaId { get; set; }
[ForeignKey("CinemaId")]
public Cinema Cinema { get; set; }
//Producer
public int ProducerId { get; set; }
[ForeignKey("ProducerId")]
public Producer Producer { get; set; }
}
}
namespace eTicekts.Models
{
public class Producer
{
[Key]
public int Id { get; set; }
public string ProfilePictureURL { get; set; }
public string FullName { get; set; }
public string Bio { get; set; }
//relationships
public List<Movie> Movies { get; set; }
}
}
I couldn't resolve the error "The relationship from 'Movie.Cinema' to 'Cinema.Movies' with foreign key properties {'CinemaId' : int} cannot target the primary key {'Logo' : string} because it is not compatible. Configure a principal key or a set of foreign key properties with compatible types for this relationship." and I'm reaching out for help to solve it.
You are missing the Id
in Cinema, the Cinema class should be like this.
public class Cinema
{
[Key]
public int Id { get; set; }
public string Logo { get; set; }
public string Name { get; }
public string Description { get; }
//relationships
public List<Movie> Movies { get; set; }
}