简体   繁体   中英

.Net migration The entity type 'List<int>' requires a primary key to be defined

Hello guys I have a problem with migraiton.I use abstract class like:

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
        [ForeignKey("UserId")]
        public int UserId { get; set; }
        public DateTime? DateTime { get; set; }
        public virtual User User { get; set; }

Then I have 2 tables like as follows:

public class MovieFavoriteList:FavoriteListBase<int>
    {
        public List<int> MovieIds { get; set; }

    }
public class TvSeriesFavoriteList:FavoriteListBase<int>
    {
        
        public List<int> TvIds { get; set; }
    }

When I try to migrate this error occurs:

The entity type 'List<int>' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943 .

Do you guys have an idea about how can I fix it?

I try as follows:

 [Keyless]
    public class TvSeriesFavoriteList:FavoriteListBase<int>
    {
        
        public List<int> TvIds { get; set; }
    }

however then I realized it was nonsense:

It gives this error because the primary key cannot be found. If you want to use the default convention, use this attribute for the attributes you want to make primary key.

[ForeignKey(nameof(MovieIds)]
public int MovieIds {get;set;}

OR For Fluent API:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
   base.OnModelCreating(modelBuilder);
   modelBuilder.Entity<MovieFavoriteList>().HasKey(p=>p.MovieIds)
}

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