Should I have a foreign key column in my entity models?

I am coming from the Entity Framework in NHibernate. When I look at how to create domain objects, I noticed that in some examples they do not include a foreign key relationship column. Since the class Sessioncontains a method Load(), you can simply use objects without going to the database instead of primary keys. Is this common practice when creating entity models in NHibernate.

Object Example

public class BlogPost : Entity
{
    public virtual string Name { get; set; }

    //Should this be here
    public virtual int AuthorID { get; set; }

    public virtual Author Author { get; set; }
}

Object Creation

BlogPost post = new BlogPost
{
    Name = "My first post",
    Author = session.Load<Author>(1) //Avoids hitting the database
};

session.Save(post);

- OR ---

BlogPost post = new BlogPost
{
    Name = "My first post",
    AuthorID = 1 //Use the id of the object
};

session.Save(post);
+5
source share
1 answer

You should use full objects / objects instead of foreign keys.

- . , - . . Blog Posts. Post Blog ..

.

Object-Relational Mapping , (Object), (Relational) ( , Mapping ).

ORM , . : NHibernate.

+3

All Articles