如何有没有数据库导航属性 [英] How to have navigation properties without a database

查看:205
本文介绍了如何有没有数据库导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的建筑,没有持久性数据的应用程序。所以这将是在存储器

I am building an app with NO persistence data. So it will be in memory.

我有以下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; }

    }

我已经创建,将填充一些示例数据的类

I have created a class that will populated some sample data

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

    }

数据访问code

data access code

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;

        }



    }

控制器code

controller code

public class BookController : Controller
    {
        private IBookRepository _bookRepo;

        public BookController(IBookRepository bookRepo)
        {

            _bookRepo = bookRepo;
        }

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

            return View(test);
        }
}

但是,当我从储备库中获得数据,导航性能是空的......我究竟做错了什么?

But when I get the data from the repository, the navigation properties are empty... what am I doing wrong?

推荐答案

您需要填写的书籍收藏在 DemoData 。如果你不自己设定他们,他们都为空。因此,在短期,如果你不使用任何持久性框架必须建立双方的关系。

You need to fill collections for books in DemoData. If you don't set them yourself they are null. So in short if you don't use any persistence framework you must create relations from both sides.

例如对于 BOOK1 你需要添加:

book1.BorrowedBooks.Add(borrowed4);

等了所有的实体内存数据库的所有集合在你的。

And so on for all collections in all entities in your in memory database.

这篇关于如何有没有数据库导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆