Asp.net mvc 外键在MVC中无效
我这里有代码优先的方法。我在下面列出了这些型号Asp.net mvc 外键在MVC中无效,asp.net-mvc,entity-framework,asp.net-mvc-5,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我这里有代码优先的方法。我在下面列出了这些型号 public class BookInfo { [Key] public int BookInfoid { get; set; } public string ISBN { get; set; } public string Title { get; set; } [ForeignKey("Publisher")] public int Publisherid { get; set; } pu
public class BookInfo
{
[Key]
public int BookInfoid { get; set; }
public string ISBN { get; set; }
public string Title { get; set; }
[ForeignKey("Publisher")]
public int Publisherid { get; set; }
public string Edition { get; set; }
public string Printing { get; set; }
public int Pages { get; set; }
public string Language { get; set; }
public string Summary { get; set; }
public string CoberPage { get; set; }
public DateTime DatePublished { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Thikness { get; set; }
public float Weight { get; set; }
public string AmazonLink { get; set; }
//public Transaction Transaction { get; set; }
// public BookByAuthor BookByAuthor { get; set; }
// public ICollection<BookByCategory> BookByCategory { get; set; }
public Publisher Publisher { get; set; }
public LibraryBook LibraryBook { get; set; }
}
public class BookStatus
{
[Key]
public int BookStatusid { get; set; }
public string Name { get; set; }
public LibraryBook LibraryBook { get; set; }
}
public class LibraryBook
{
[Key]
public int LibraryBookid { get; set; }
[ForeignKey("BookInfo")]
public int BookInfoid { get; set; }
[ForeignKey("BookStatus")]
public int BookStatusid { get; set; }
public float Price { get; set; }
public DateTime ObtainedFrom { get; set; }
//public ICollection<Transaction> Transactions { get; set; }
public IEnumerable<BookStatus> BooksStatus { get; set; }
public IEnumerable<BookInfo> BooksInfo { get; set; }
}
我的DbContext是
public class DBContext:DbContext
{
public DbSet<City> Cities { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<Publisher> Publishers { get; set; }
public DbSet<BookInfo> BooksInfo { get; set; }
public DbSet<Member> Members { get; set; }
public DbSet<BookStatus> BooksStatus { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<LibraryBook> LibraryBooks { get; set; }
// public DbSet<Transaction> Transactions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
当我尝试添加一个控制器时,我得到一个错误,如下所示。我使用的是MVC5代码优先的方法
**无法检索Library\u management\u Syatem.LibraryBook的元数据。类型“Library\u management\u Syatem.LibraryBook”的属性“BookInfoid”上的Foreignkey属性无效。在依赖类型“Library\u management\u Syatem.LibraryBook”上找不到导航属性“BookInfoid”。名称值应为有效的导航属性
请帮助我**您在错误的属性上设置了ForeignKey属性。对于发布者来说,这应该是这样的,我认为在复杂类型上添加虚拟元素会有所帮助:
public class BookInfo
{
[Key]
public int BookInfoid { get; set; }
public string ISBN { get; set; }
public string Title { get; set; }
public int Publisherid { get; set; }
public string Edition { get; set; }
public string Printing { get; set; }
public int Pages { get; set; }
public string Language { get; set; }
public string Summary { get; set; }
public string CoberPage { get; set; }
public DateTime DatePublished { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Thikness { get; set; }
public float Weight { get; set; }
public string AmazonLink { get; set; }
//public Transaction Transaction { get; set; }
// public BookByAuthor BookByAuthor { get; set; }
// public ICollection<BookByCategory> BookByCategory { get; set; }
[ForeignKey("Publisherid")]
public virtual Publisher Publisher { get; set; }
public LibraryBook LibraryBook { get; set; }
}