views:

222

answers:

1

I am trying to learn Nhibernate using fluent nhibernate mappings and i have created a small test app. Below is the code:

UserClass:

public class User
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Role { get; set; }
    public string Email { get; set; }
    public DateTime JoinDate { get; set; }
}

User Map Class:

public class UserMapping: ClassMap<User>
{
    public UserMapping()
    {
        WithTable("UserT");
        Not.LazyLoad();
        Id(u => u.UserName);
        Map(u => u.Password);
        Map(u => u.Role);
        Map(u => u.Email);
        Map(u => u.JoinDate);

    }
}

DAL User Class:

public class DALUser
{

    public User GetUserByUserName(string userName)
    {
        ISessionFactory sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.Server("ServerName").Database("DBName").TrustedConnection()))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMapping>()).BuildSessionFactory();

        ISession session = sessionFactory.OpenSession();
        return session.Get<User>(userName);
    }

}

Nunit Test Class:

[TestFixture]
public class UserTest
{
    [Test]
    public void CanGetUserByUserName()
    {
        DALUser user1 = new DALUser();
        Assert.AreEqual("testUser", user1.GetUserByUserName("testUser").UserName);
    }

}

When i try to run the test class i get the following error :Object reference not set to an instance of an object. I have placed a breakpoint in the GetUserByUserName method and noticed that its returning a null user. But i am not able to figure out why this is happening. Can anyone help me?

A: 

Hi!

Take a look is your database configuration correct? Are you sure that you database server is "ServerName" and you have existing Database "DBName"

Lauri Lüüs