Я новичок в MVC, и я следую руководству по созданию проекта MVC с первым кодом и Аутентификация отдельных учетных записей пользователей в Интернете.Но когда я изменяю модель на отношение «многие ко многим», я получаю
MyFirstASP_MVC_API.Models.BookAuthor:: EntityType «BookAuthor» не имеет определенного ключа.Определите ключ для этого EntityType.BookAuthors: EntityType: EntitySet «BookAuthors» основан на типе «BookAuthor», для которого не определены ключи.
Вот мои классы
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использовал ApplicationDbContext, который находится в 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();
}
}
Я читал статью о решении проблемы, они использовали OnModelCreating
, но ни один из них не использовал ApplicationDbContext
, так что я не знаю, должен ли ядействительно добавьте его в ApplicationDbContext
.Так что мне делать, нужно ли создавать новый DbContext ???