0

I use EF6 and these are my POCO's.

public class Author
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

public class Book
    {
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        public int Year { get; set; }
        public decimal Price { get; set; }
        public string Genre { get; set; }

        // Foreign Key
        public int AuthorId { get; set; }
        // Navigation property
        public Author Author { get; set; }
    }
  1. What is the point of the navigation property Author?
  2. What is the point of making the property virtual?
  3. Are navigation properties specific to Entity Framework?

EDIT: after a bit more research I've found this thread, which nicely answer my first question.

Can people help with the last two questions please?

Community
  • 1
  • 1
U r s u s
  • 6,680
  • 12
  • 50
  • 88
  • 2
    [This may help](https://www.google.com.au/webhp?sourceid=chrome-instant&rlz=1C1CHMD_enAU565AU565&ion=1&espv=2&es_th=1&ie=UTF-8#q=entity%20framework%20navigation%20properties&es_th=1). –  Jan 23 '15 at 11:44
  • 1
    @MickyDuncan [Embrace the non-Googlers](http://meta.stackexchange.com/q/5280/167646). Stack Overflow questions often end up as one of the top results in search engines, so sending people back to Google is not helpful. – user247702 Jan 23 '15 at 11:49
  • 1
    @Stijn Perhaps, but poster shows `no effort of prior research` and besides, using the OP's exact title _[What do navigation properties do in Entity Framework?](https://www.google.com.au/search?q=What+do+navigation+properties+do+in+Entity+Framework&rlz=1C1CHMD_enAU565AU565&oq=What+do+navigation+properties+do+in+Entity+Framework&aqs=chrome..69i57.1119j0j4&sourceid=chrome&es_sm=122&ie=UTF-8)_ –  Jan 23 '15 at 11:54
  • The real question is bearing in mind that of course an author has a book, but can a book have multiple authors? (just as an Author can write multiple books) :D Many to Many is bad mmkay. – Paul Zahra Jan 23 '15 at 11:57
  • @Ursus take a look at https://msdn.microsoft.com/en-us/data/jj713564.aspx – RyanCJI Jan 23 '15 at 12:03
  • @MickyDuncan thanks I've seen that (surprise!). The link doesn't address questions 2 and 3 though. – U r s u s Jan 23 '15 at 12:05
  • @Ryan thanks, read the previous comments. – U r s u s Jan 23 '15 at 12:06
  • @PaulZahra I am assuming one book only has one author. It's a sample app. – U r s u s Jan 23 '15 at 12:07
  • possible duplicate of [What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?](http://stackoverflow.com/questions/5597760/what-effects-can-the-virtual-keyword-have-in-entity-framework-4-1-poco-code-fi) – CodeCaster Jan 23 '15 at 12:10
  • 1
    [Entity Framework 4.1 Virtual Properties](http://stackoverflow.com/questions/7738722/entity-framework-4-1-virtual-properties). – CodeCaster Jan 23 '15 at 12:11

1 Answers1

1

2: if you do virtual navigation properties then will work lazy load value of this property:

public class Book
{
    //...
    public int AuthorId { get; set; }
    public virtual Author Author { get; set; }
}

It work: EF create proxy to your entity (Book) and overrides navigation property (Author). When you load entity (Book) of the database value of the navigation property (Author) is not load. Only the first get value of navigation property (Author), it will be load (http://blogs.msdn.com/b/adonet/archive/2009/12/22/poco-proxies-part-1.aspx).

3: inherently navigation properties is no specific to Entity Framework. This link to other entities. But term "navigation properties" is specific to EF.

sribin
  • 1,736
  • 1
  • 13
  • 21