0

I have two following classes:

public class User
{
    public virtual Guid Id { get; set; }
    public virtual UserCredentials Credentials { get; set; }

    // other stuff

    protected User() { }
}

public class UserCredentials
{
    public virtual Guid Id { get; set; }
    public virtual string UserName { get; set; }

    // other stuff

    protected UserCredentials() { }
}

I want to create a detached criteria that finds all users where Credentials.UserName = "someuser", but I cannot get it right.. I have tried the following:

DetachedCriteria.For<User>().Add(Expression.Eq("Credentials.UserName", "someuser");

but I get an exception saying

"could not resolve property: Credentials.UserName of: DataLinkNord.Domain.User"

Any help would be appreciated..

Morten Jacobsen
  • 986
  • 1
  • 11
  • 31

1 Answers1

2

I've run into this issue before myself (although I'm a Java user), but the way around it for me was to use the "addAlias()" call first... something like:

DetachedCriteria.For<User>().AddAlias("Credentials", "Credentials").Add(Expression.Eq("Credentials.UserName", "someuser");
BryanD
  • 1,897
  • 12
  • 13
  • Okay thanks, but I think I need to revisit my mappings, because I get an invalid SQL expression where it tries to join the User table with itself.. – Morten Jacobsen Dec 15 '09 at 19:53