外键在MVC中无效

时间:2014-12-17 09:58:27

标签: asp.net-mvc entity-framework asp.net-mvc-5

我这里有Code First Approach。我列出了以下模型

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_management_Syatem.LibraryBook的元数据。属性'BookInfoid'on类型'Library_management_Syatem.LibraryBook'上的Foreignkey属性无效。在依赖类型“Library_management_Syatem.LibraryBook”上找不到导航属性“BookInfoid”。名称值应该是有效的导航属性。

请帮助我**

1 个答案:

答案 0 :(得分:0)

你在错误的属性上有“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; }
}