Search by user type column in nhibernate

I have a User object that contains a Status property of type DictionaryItem , and this displays as UserType . Now I want to make the following linq statement:

 Session.Query<User>().Where(x => x.LoginStatus.Code == "").ToList(); 

I got the following exception:

Additional information: failed to resolve property: Code: User

I know that the problem is that I search by my custom type (the Code property exists in my DictionaryItem type). The nCHernate session query generates an SQL query when calling ToList() , but LoginStatus not a reference type, only a User type, is there a workaround to query the query by user type?

Edit 1: Below source code:

  public class User { public virtual Guid Id { get; set; } public virtual DictionaryItem LoginStatus { get; set; } public User() { } } public class UserMap : ClassMap<User> { public UserMap() { Id(x => x.Id); Map(x => x.LoginStatus).CustomType<DictionaryItemCustomType>(); } } public class DictionaryItem { public virtual int Id { get; set; } public virtual string Code { get; set; } public virtual string Description { get; set; } } public class DictionaryItemCustomType : IUserType { public new bool Equals(object x, object y) { if (x == null && y == null) { return true; } if (x == null || y == null) { return false; } return ((DictionaryItem)x).Id == ((DictionaryItem)y).Id; } public int GetHashCode(object x) { return x.GetHashCode(); } public object NullSafeGet(IDataReader rs, string[] names, object owner) { object value = NHibernateUtil.Int32.NullSafeGet(rs, names); if (value == null) { return null; } return AutofacServiceHostFactory.Container.Resolve<IDictionaryRepository>().DictionaryItems.First(x => x.Id == (int)value); } public void NullSafeSet(IDbCommand cmd, object value, int index) { DictionaryItem dictionaryItem = value as DictionaryItem; NHibernateUtil.Int32.NullSafeSet(cmd, dictionaryItem == null ? (object)null : dictionaryItem.Id, index); } public object DeepCopy(object value) { return value; } public object Replace(object original, object target, object owner) { return DeepCopy(original); } public object Assemble(object cached, object owner) { throw new NotImplementedException(); } public object Disassemble(object value) { throw new NotImplementedException(); } public SqlType[] SqlTypes { get { return new[] { NHibernateUtil.Int32.SqlType }; } } public Type ReturnedType { get { return typeof(DictionaryItem); } } public bool IsMutable { get { return false; } } } 

Bd look like this:

 CREATE TABLE [dbo].[User]( [Id] [uniqueidentifier] NOT NULL, [LoginStatusId] [int] NOT NULL ) CREATE TABLE [dbo].[DictionaryItem]( [Id] [int] IDENTITY(1,1) NOT NULL, [Code] [nvarchar](20) NOT NULL, [Description] [nvarchar](200) NOT NULL, ) 
+4
source share
2 answers

Try the following:

 DictionaryItem statusAlias = null; User userAlias = null; return Session.QueryOver<User>(() => userAlias) .JoinAlias(() => userAlias.LoginStatus, () => statusAlias) .Where(() => statusAlias.Code == "") .ToList(); 
+1
source

Your display is incorrect. DictionaryItem should not display as a custom type. For me it looks like another.

 Map(x => x.LoginStatus).CustomType<DictionaryItemCustomType>(); 

The above indicates that you want to display only one column. You want to map the actual DictionaryItem object to the user. You need to do something like this:

 References(x => x.LoginStatus, "LoginStatusId"); 

Then you need to display DictionaryItem:

 public class DictionaryItemMap : ClassMap<DictionaryItem> { public DictionaryItemMap() { Id(x => x.Id); //You'll need to do something else here for identity columns Map(x => x.Code); Map(x => x.Description); } } 

After this mapping, you can run the query mentioned above, MGA.

 LoginStatus statusAlias = null; User userAlias = null; return Session.QueryOver<User>(() => userAlias) .JoinAlias(() => userAlias.LoginStatus, () => statusAlias) .Where(() => statusAlias.Code == "") .ToList(); 
0
source

Source: https://habr.com/ru/post/1396209/


All Articles