如何使用导航属性进行设置
本文关键字:设置 属性 何使用 导航 | 更新日期: 2023-09-27 17:57:58
我正在尝试在MVC4 中设置一个简单的库应用程序
我有以下实体
public class Book
{
public Book()
{
BorrowedBooks = new List<BorrowedBooks>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public virtual ICollection<BorrowedBooks> BorrowedBooks { get; set; }
}
public class Borrower
{
public Borrower()
{
BorrowedBooks = new List<BorrowedBooks>();
}
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public virtual ICollection<BorrowedBooks> BorrowedBooks { get; set; }
}
public class BorrowedBooks
{
public int Id { get; set; }
public int BookId { get; set; }
public int BorrowerId { get; set; }
public DateTime DateBorrowed { get; set; }
public virtual Book Book { get; set; }
public virtual Borrower Borrower { get; set; }
}
我设置了2个存储库
public class BookRepository : IBookRepository
{
private List<Book> books = new List<Book>
{
new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer", BorrowedBooks = new List<BorrowedBooks>{ new BorrowedBooks {BookId = 1, BorrowerId = 1, DateBorrowed = DateTime.Parse("26/03/2014")}} },
new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" },
new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" },
new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" },
new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" },
new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" },
new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" },
new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" },
};
private List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
{
new BorrowedBooks {BookId = 8, Book = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" }, BorrowerId = 2, DateBorrowed = DateTime.Parse("01/04/2014")},
new BorrowedBooks {BookId = 6, BorrowerId = 4, DateBorrowed = DateTime.Parse("08/04/2014")},
new BorrowedBooks {BookId = 2, BorrowerId = 4, DateBorrowed = DateTime.Parse("08/04/2014")},
new BorrowedBooks {BookId = 1, BorrowerId = 1, DateBorrowed = DateTime.Parse("26/03/2014")},
};
public IEnumerable<Book> Search()
{
return books;
}
}
public class BorrowerRepository : IBorrowerRepository
{
private List<Borrower> borrowers = new List<Borrower>
{
new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" },
new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" },
new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" },
new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" },
};
public void Add(Borrower borrower)
{
this.borrowers.Add(borrower);
}
}
如何将属性链接在一起?即在我的BorrowerDepository搜索方法中,它返回所有数据,但Book值只是一个ID,我如何将其与图书存储库中的值链接?
我的导航属性设置错误了吗?还是我设置存储库数据的方式?
Linq
进行查询。
public static class DataStore
{
private static List<Book> books = new List<Book>
{
new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer", BorrowedBooks = new List<BorrowedBooks>{ new BorrowedBooks {BookId = 1, BorrowerId = 1, DateBorrowed = DateTime.Parse("26/03/2014")}} },
new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" },
new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" },
new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" },
new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" },
new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" },
new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" },
new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" },
};
private static List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
{
new BorrowedBooks {BookId = 8, Book = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" }, BorrowerId = 2, DateBorrowed = DateTime.Parse("01/04/2014")},
new BorrowedBooks {BookId = 6, BorrowerId = 4, DateBorrowed = DateTime.Parse("08/04/2014")},
new BorrowedBooks {BookId = 2, BorrowerId = 4, DateBorrowed = DateTime.Parse("08/04/2014")},
new BorrowedBooks {BookId = 1, BorrowerId = 1, DateBorrowed = DateTime.Parse("26/03/2014")},
};
private static List<Borrower> borrowers = new List<Borrower>
{
new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" },
new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" },
new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" },
new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" },
};
public static List<Book> Books { get { return books; } }
public static List<BorrowedBooks> BorrowedBooks { get { return borrowedBooks; } }
public static List<Borrower> Borrowers { get { return borrowers; } }
}
public class BookRepository : IBookRepository
{
public IEnumerable<Book> Search()
{
return DataStore.Books.Where (b => b.Author == "Paul Jennings");
}
}
public class BorrowerRepository : IBorrowerRepository
{
public void Add(Borrower borrower)
{
DataStore.Borrowers.Add(borrower);
}
}