简体   繁体   中英

Code first: EntityType 'xxxx' has no key defined. Define the key for this EntityType when adding n-to-n relationship

I'm new to MVC and I'm following a guide to make a MVC project with code first and Individual User Accounts Authentication on the Internet. But when I change the model to a many-to-many relationship, I get the

MyFirstASP_MVC_API.Models.BookAuthor: : EntityType 'BookAuthor' has no key defined. Define the key for this EntityType. BookAuthors: EntityType: EntitySet 'BookAuthors' is based on type 'BookAuthor' that has no keys defined.

Here are my classes

public class Author
    {
        public int Id { get; set; }

        [Required]
        [StringLength(100)]
        public string Name { get; set; }

        [StringLength(1000)]
        public string Description { get; set; }

        public bool IsActive { get; set; }


        public ICollection<BookAuthor> BookAuthors { get; set; }
    }
public class Book
    {
        public int Id { get; set; }

        [Required]
        [StringLength(255)]
        public string Name { get; set; }

        [Required]
        [Index(IsUnique = true)]
        [StringLength(20)]
        public string ISBN { get; set; }

        public decimal Price { get; set; }

        public int Quantity { get; set; }

        public Nullable<DateTime> CreatedDate { get; set; }

        public Nullable<DateTime> ModifiedDate { get; set; }

        public bool IsActive { get; set; }

        public ICollection<BookAuthor> BookAuthors { get; set; }

        public ICollection<BookCategory> BookCategories { get; set; }

        public Publisher Publisher { get; set; }
        public int PublisherId { get; set; }
    }
public class BookAuthor
    {
        public int BookId { get; set; }
        public Book Book { get; set; }

        public int AuthorId { get; set; }
        public Author Author { get; set; }
    }

I used ApplicationDbContext which is in IdentityModel.cs

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<Book> Books { get; set; }
        public DbSet<Author> Authors { get; set; }
        public DbSet<Publisher> Publishers { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<BookCategory> BookCategories { get; set; }
        public DbSet<BookAuthor> BookAuthors { get; set; }
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

I've read some article about solving the problem, they used OnModelCreating but none of them using the ApplicationDbContext so I don't know I should really add it into the ApplicationDbContext . So what should I do, do I need to create new DbContext ???

An Author can have multiple books so you should define a collection of Books in your Author Entity:

public ICollection<Book> Books { get; set; }

A book can actually have more than 1 Author in reality (not sure if it is the same in your case) but if it is, you should declare a collection of Authors in your Book Entity:

public ICollection<Author> Authors { get; set; }

Thats it, this will yield you the following tables: 在此处输入图片说明

In addition, if you want to learn more there are really a ton of sources available:

Good luck!

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