locked
EF code first and FK mention RRS feed

  • Question

  • User1183902823 posted

    just view this link http://www.learnentityframeworkcore.com/configuration/data-annotation-attributes/foreignkey-attribute

    public class Book
    {
        public int BookId { get; set; }
        public string Title { get; set; }
        public Author Author { get; set; }
        [ForeignKey("Author")]
        public int AuthorFK { get; set; }
    }
    
    public class Author
    {
        public int AuthorId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        [ForeignKey("AuthorFK")]
        public ICollection<Book> Books { get; set; }
    }
    

    why do we need to mention FK columns in two model class ? is it necessary?

    what the article try to explain?

    if i mention FK in books class does it work or do i need to mention FK in book and author both class ?

    please guide.

    Wednesday, December 13, 2017 10:41 AM

Answers

  • User475983607 posted

    why do we need to mention FK columns in two model class ? is it necessary?

    what the article try to explain?

    if i mention FK in books class does it work or do i need to mention FK in book and author both class ?

    So the navigation can go both ways.  The Book class can have one Author and the Author class cab have many Books.

    As with many of your recent posts, please take the time to read the MS reference documentation.  Are you having trouble finding the reference documentation?

    This tutorial explains how to use EF in ASP Core

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/

    This link explains relationships, see the menu on the left for more information.

    https://docs.microsoft.com/en-us/ef/core/modeling/relational/fk-constraints

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 13, 2017 12:16 PM