EntityType has no key defined, mimo, że użyłem atrybutu [Key]

0

Konfiguruję sobie context w mojej aplikacji.

public class BookListContext : IdentityDbContext
    {
        public BookListContext()
            : base("DefaultConnection")
        {
        }

        public static BookListContext Create()
        {
            return new BookListContext();
        }

        **public DbSet<ApplicationUser> ApplicationUsers { get; set; }
        public DbSet<Book> Books { get; set; }**

        //protected override void OnModelCreating(DbModelBuilder modelBuilder)
        //{
        //    base.OnModelCreating(modelBuilder);
        //    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        //    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            
        //}
    }

Pogrubione linijki rzucają wyjątek.

BookList.Models.Book: : EntityType 'Book' has no key defined. Define the key for this EntityType.
Books: EntityType: EntitySet 'Books' is based on type 'Book' that has no keys defined. 

Szukałem na StackOverflow, dodałem atrybut [Key] przy właściwości BookId. Nic to nie dało. Załączam klasę User oraz Book.

BOOK:

 public class Book
    {
        [Display(Name = "Id:")]
        [Key]
        private int BookId { get; set; }

        [Display(Name = "Title:")]
        [MaxLength(35)]
        [Required]
        private string Title { get; set; }

        [Display(Name = "Description:")]
        [MaxLength(300)]
        private string Description { get; set; }
    }

USER:

 public class ApplicationUser : IdentityUser
    {
        public ApplicationUser()
        {
            this.FavouriteBooks = new HashSet<Book>();
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual ICollection<Book> FavouriteBooks { get; private set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

Liczę na pomoc, @Shalom, kiedyś mnie ojebałeś za źle napisany temat, teraz dobrze? :)

0

Zprywatyzowałem propertisy. To rozwiązanie. Już są wolne! :)

1

Popróbuj

[Key]
public int BookId { get; set; }

1 użytkowników online, w tym zalogowanych: 0, gości: 1