ale
ale

Reputation: 3431

InvalidCastException NHibernate

Table EmployeebySet: [**IdEmployee, IdSet**, Name, Date]
Table Employee: [IdEmployee, Name, Age...]
Table Set: [IdSet, Date, Name...]

I'm work with NHibernate, I have some tables, and making the test with NUnit. So, I have a problem in this method, when I want to obtain some data in table EmployeebySet by IdEmployee or IdSet. I think the problem is because the PrimaryKey is a composite. (IdEmployee, IdSet)

public EmployeebySet GetEmployeebySet_byIdEmployee(int IdEmployee) 
        {
            using (ISession session = NHibernateSessionBuilder.OpenSession())
                return session.Get<EmployeebySet>(IdEmployee);
        }

But when I give IdEmployee = 3, in the last line occurs this error InvalidCastException System.Int32

What can I do?.

Upvotes: 0

Views: 811

Answers (1)

Lester
Lester

Reputation: 4413

Since you're dealing with composite keys you will have to structure your test to pass both keys in:

public EmployeebySet GetEmployeebySet_byIdEmployeeAndIdSet(int IdEmployee, int IdSet)
{
    using (ISession session = NHibernateSessionBuilder.OpenSession())
    {
        return session.Get<EmployeebySet>(
            new EmployeebySet 
            {
                IdEmployee = IdEmployee,
                IdSet = IdSet
            });
    }
}

Upvotes: 1

Related Questions