Reputation: 841
I cannot find an example on this. I'm trying to get a basic understanding of Fluent NHibernate but resources seem quite scarce in terms of proper tutorials.
I have a test class like this:
public class User
{
public virtual long ID { get; set; }
public virtual string Username { get; set; }
public virtual MoreDetails ExtendedDetails { get; set; }
}
With another class like this:
public class MoreDetails
{
public virtual long ID { get; set; }
public virtual string Firstname { get; set; }
public virtual long UserID { get; set; } // Foreign key in the DB
}
What exactly should my mapping look like?
How can I query the DB properly with either Lazy or Eager loading to be able to do this:
// user object instantiated using your provided example:
userObject.ExtendedDetails.Firstname
I feel like an idiot.. normally I can follow documentation but its very vague with this sort of usage. Can anyone point me to a proper example (or give one)?
I'm using the latest Fluent NHibernate straight from the Fluent NHibernate website.
Regards,
chem
Upvotes: 2
Views: 274
Reputation: 8393
Here is a good walk through that can help you get up and running: http://dotnetslackers.com/articles/ado_net/Your-very-first-NHibernate-application-Part-1.aspx
Your mappings would look something like this. Note, that I'm making some assumptions on that how you would like to generate your identities along with the type of mapping between these entities.
public class User
{
public int ID { get; set; }
public string Username { get; set; }
public MoreDetails ExtendedDetails { get; set; }
}
public class MoreDetails
{
public int ID { get; set; }
public string Firstname { get; set; }
public User User { get; set; } // Foreign key in the DB
}
public UserMapping()
{
Not.LazyLoad();
Id(e => e.ID).GeneratedBy.Identity();
Map(e => e.Username).Not.Nullable();
HasOne(x => x.ExtendedDetails)
.Cascade
.SaveUpdate();
}
public MoreDetailsMapping()
{
Not.LazyLoad();
Id(e => e.ID).GeneratedBy.Identity();
Map(e => e.Firstname).Not.Nullable();
References(x => x.User).Column("UserID");
}
In order to query your relational data you will need to open a session within nhibernate. I generally create a helper along the lines of this.
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
InitializeSessionFactory();
return _sessionFactory;
}
}
private static void InitializeSessionFactory()
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(@"Server=localhost\SQLExpress;Database=SomeDB;Trusted_Connection=True;")
.ShowSql()
)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<User>())
.BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
From there you could directly query like this:
public IQueryable<User> Users
{
get { return NHibernateHelper.OpenSession().Query<User>(); }
}
Note, that I'm leaving a lot out of this but hopefully this will assist you in getting up and running.
Upvotes: 2
Reputation: 2489
If your db really does have a one-to-one mapping between two tables you need to use HasOne in your ClassMap e.g. HasOne(x => x.ExtendedDetails)
See http://wiki.fluentnhibernate.org/Fluent_mapping for details on One-to-One mappings
Upvotes: 0