How to resolve the 'foreign key not compatible' error when using 'add migration Initial' in ASP.NET Core migration?

huangapple go评论75阅读模式
英文:

How to resolve the 'foreign key not compatible' error when using 'add migration Initial' in ASP.NET Core migration?

问题

在我进行电子门票项目时,我尝试使用 "add migration Initial",但遇到了以下错误:“Movie.Cinema”到“Cinema.Movies”的关系,外键属性为{ 'CinemaId':int },无法指向主键{ 'Logo':string },因为它们不兼容。为此关系配置主键或一组具有兼容类型的外键属性。然而,根据我观看的教程,所有属性(如CinemaId)似乎都是相同的,所以我不确定如何解决它。以下是我的代码:

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> Cinemas { 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; set; }

        public string Description { get; set; }
        //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; }
    }

}

我无法解决错误 "Movie.Cinema 到 Cinema.Movies 的关系,外键属性为 { 'CinemaId':int },无法指向主键 { 'Logo':string },因为它们不兼容。请帮助我解决这个问题。

英文:

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:

<!-- begin snippet: js hide: false console: true babel: false -->

namespace eTicekts.Data
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions&lt;AppDbContext&gt;options):base(options) 
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity&lt;Actor_Movie&gt;().HasKey(am =&gt; new
{
am.ActorId,
am.MovieId
});
modelBuilder.Entity&lt;Actor_Movie&gt;().HasOne(m=&gt;m.movie).WithMany(am=&gt;am.actor_Movies).HasForeignKey(am=&gt;am.MovieId);
modelBuilder.Entity&lt;Actor_Movie&gt;().HasOne(m =&gt; m.actor).WithMany(am =&gt; am.actor_Movies).HasForeignKey(am =&gt; am.ActorId);
base.OnModelCreating(modelBuilder);
}
public DbSet&lt;Actor&gt; Actors { get; set; }
public DbSet&lt;Movie&gt; Movies { get; set; }
public DbSet&lt;Actor_Movie&gt; Actor_Movies { get; set; }
public DbSet&lt;Cinema&gt; Cinmeas { get; set; }
public DbSet&lt;Producer&gt; 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&lt;Actor_Movie&gt; 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&lt;Movie&gt; Movies { get; set; }
}
}
{
public class ErrorViewModel
{
public string? RequestId { get; set; }
public bool ShowRequestId =&gt; !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&lt;Actor_Movie&gt; actor_Movies { get; set; }
//cinema
public int CinemaId { get; set; }
[ForeignKey(&quot;CinemaId&quot;)]
public Cinema Cinema { get; set; }
//Producer
public int ProducerId { get; set; }
[ForeignKey(&quot;ProducerId&quot;)]
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&lt;Movie&gt; Movies { get; set; }
}
}

<!-- end snippet -->

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.

答案1

得分: 0

正如您可以从错误消息中看到的那样,Cinema表的关键是“string Logo { get; set; }”,它是字符串,您正在引用自电影表的“int CinemaId { get; set;}”,它是整数,所以您可以向Cinema表中添加cinemaid列并将其设为关键,或者您可以将电影表中的cinemaid数据类型更改为字符串。

英文:

as you can see from the error Cinema table's Key is "string Logo { get; set; }"
which is string , and you Referring from movies table "int CinemaId { get; set;}"
which is int , so you can Add column cinemaid to cinema Table and make it Key , or you can change cinemaid's datatype in Movies as string.

答案2

得分: 0

你的Cinema类缺少Id字段,应该像这样修改:

public class Cinema
{
    [Key]
    public int Id { get; set; }

    public string Logo { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    // 关联关系
    public List<Movie> Movies { get; set; }
}
英文:

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&lt;Movie&gt; Movies { get; set; }
}

huangapple
  • 本文由 发表于 2023年6月1日 00:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定