Tuesday, August 21, 2012

ORM navigation differences between Entity Framework and NHibernate

Given this model:

public class Person
{
 public virtual int PersonId { get; set; }
 public virtual string PersonName { get; set; }
}


public class Question
{
 public virtual int QuestionId { get; set; }
 public virtual string QuestionText { get; set; }
 public virtual Person AskedBy { get; set; }
}


And this schema:

create table Person
(
PersonId int identity(1,1) primary key,
PersonName nvarchar(100) not null
);

create table Question
(
QuestionId int identity(1,1) primary key,
QuestionText nvarchar(100) not null,
AskedBy_PersonId int not null references Person(PersonId)
);



While this incur two database roundtrip on Entity Framework:

Console.WriteLine("{0}", q.QuestionText);
Console.WriteLine("{0}", q.AskedBy.PersonId);


NHibernate doesn't, it incurs one database roundtrip only

No comments:

Post a Comment