如何在没有数据库的情况下获得导航属性

时间:2014-04-12 16:39:14

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

我正在构建一个没有持久性数据的应用。所以它将在记忆中。

我有以下POCO实体

 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; }

    }

我创建了一个将填充一些示例数据的类

public class DemoData
    {
        static Book book1 =    new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer" };
        static Book book2 = new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" };
        static Book book3 = new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" };
        static Book book4 = new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" };
        static Book book5 = new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" };
        static Book book6 = new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" };
        static Book book7 = new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" };
        static Book book8 = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" };


        static Borrower borrower1 = new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" };
        static Borrower borrower2 = new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" };
        static Borrower borrower3 = new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" };
        static Borrower borrower4 = new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" };


        static BorrowedBooks borrowed1 = new BorrowedBooks { BookId = 8, Book = book8,  BorrowerId = 2, Borrower=borrower2, DateBorrowed = DateTime.Parse("01/04/2014") };
        static BorrowedBooks borrowed2 =   new BorrowedBooks {BookId = 6, Book = book6,  BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014")};
        static BorrowedBooks borrowed3 = new BorrowedBooks { BookId = 2, Book = book2, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014") };
        static BorrowedBooks borrowed4 = new BorrowedBooks { BookId = 1, Book = book1, BorrowerId = 1, Borrower = borrower1, DateBorrowed = DateTime.Parse("26/03/2014") };

        public List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
            {
                borrowed1, borrowed2, borrowed3, borrowed4

            };


        public List<Book> books = new List<Book>
            {
               book1, book2, book3, book4, book5, book6, book7, book8

            };

        private List<Borrower> borrowers = new List<Borrower>
            {
                borrower1, borrower2, borrower3, borrower4              
            };

    }

数据访问代码

public class BookRepository : IBookRepository
    {

        private DemoData data = new DemoData();



        public bool Add(Book book)
        {
            try
            {
                this.data.books.Add(book);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public bool BorrowBook(BorrowedBooks details)
        {

            try
            {
                this.data.borrowedBooks.Add(details);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;


        }

        public IEnumerable<Book> Search()
        {

            return data.books;

        }



    }

控制器代码

public class BookController : Controller
    {
        private IBookRepository _bookRepo;

        public BookController(IBookRepository bookRepo)
        {

            _bookRepo = bookRepo;
        }

        public ActionResult Search()
        {
            var test = _bookRepo.Search();

            return View(test);
        }
}

但是当我从存储库中获取数据时,导航属性是空的......我做错了什么?

2 个答案:

答案 0 :(得分:1)

您需要在DemoData中填写图书集。如果您没有自己设置它们,则它们为空。所以简而言之,如果你不使用任何持久性框架,你必须从双方创建关系。

例如,对于book1,您需要添加:

book1.BorrowedBooks.Add(borrowed4);

依此类推内存数据库中所有实体的所有集合。

答案 1 :(得分:0)

我认为您还需要在DemoData类中使列表静态,并从您的存储库中删除DemoData的实例私有成员。这样,所有存储库都将使用相同的数据。

public static class DemoData
{
    static Book book1 =    new Book { Id = 1, Title = "Queen of the road", Author = "Tricia Stringer" };
    static Book book2 = new Book { Id = 2, Title = "Don't look now", Author = "Paul Jennings" };
    static Book book3 = new Book { Id = 3, Title = "Too bold to die", Author = "Ian McPhedran" };
    static Book book4 = new Book { Id = 4, Title = "The rosie project", Author = "Graeme Simson" };
    static Book book5 = new Book { Id = 5, Title = "In great spirits", Author = "Archie Barwick" };
    static Book book6 = new Book { Id = 6, Title = "The vale girl", Author = "Nelika Mcdonald" };
    static Book book7 = new Book { Id = 7, Title = "Watching you", Author = "Michael Robotham" };
    static Book book8 = new Book { Id = 8, Title = "Stillways", Author = "Steve Bisley" };


    static Borrower borrower1 = new Borrower { Id = 1, Firstname = "John", Lastname = "Smith" };
    static Borrower borrower2 = new Borrower { Id = 2, Firstname = "Mary", Lastname = "Jane" };
    static Borrower borrower3 = new Borrower { Id = 3, Firstname = "Peter", Lastname = "Parker" };
    static Borrower borrower4 = new Borrower { Id = 4, Firstname = "Eddie", Lastname = "Brock" };


    static BorrowedBooks borrowed1 = new BorrowedBooks { BookId = 8, Book = book8,  BorrowerId = 2, Borrower=borrower2, DateBorrowed = DateTime.Parse("01/04/2014") };
    static BorrowedBooks borrowed2 =   new BorrowedBooks {BookId = 6, Book = book6,  BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014")};
    static BorrowedBooks borrowed3 = new BorrowedBooks { BookId = 2, Book = book2, BorrowerId = 4, Borrower = borrower4, DateBorrowed = DateTime.Parse("08/04/2014") };
    static BorrowedBooks borrowed4 = new BorrowedBooks { BookId = 1, Book = book1, BorrowerId = 1, Borrower = borrower1, DateBorrowed = DateTime.Parse("26/03/2014") };

    public static List<BorrowedBooks> borrowedBooks = new List<BorrowedBooks>
        {
            borrowed1, borrowed2, borrowed3, borrowed4

        };


    public static List<Book> books = new List<Book>
        {
           book1, book2, book3, book4, book5, book6, book7, book8

        };

    private static List<Borrower> borrowers = new List<Borrower>
        {
            borrower1, borrower2, borrower3, borrower4              
        };
}

然后在您的图书库中,您可以通过静态成员而不是实例字段进行访问。

public class BookRepository : IBookRepository
{
    public bool Add(Book book)
    {
        try
        {
            DemoData.books.Add(book);
        }
        catch (Exception ex)
        {
            return false;
        }

        return true;
    }

    public bool BorrowBook(BorrowedBooks details)
    {

        try
        {
            DemoData.borrowedBooks.Add(details);
        }
        catch (Exception ex)
        {
            return false;
        }

        return true;