如何使用导航属性进行设置

时间:2014-04-12 15:05:21

标签: c# .net asp.net-mvc-3 asp.net-mvc-4

我正在尝试在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);
        }
    }

如何将属性链接在一起?即在我的BorrowerRepository搜索方法中,它返回所有数据,但Book值只是一个ID,如何将其与书库中的值链接?

我的导航属性设置错了吗?还是我设置我的存储库数据的方式?

1 个答案:

答案 0 :(得分:0)

实现这一目标的一种方法是添加一个静态类来将数据集合保存在内存中。 然后,在每个存储库中,您将任何与数据相关的操作委派给数据存储中的相应集合,并且可以使用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);
    }
}