Hello,
I'm trying to find a user with a specific username. I get a null reference exception if I query the user as follows:
var users = unitOfWork.Query<User>().Where(e => e.Username == username);
The exception looks as follows:
Mindscape.LightSpeed.Model.TypeModel.FindValueModel(String name)
Mindscape.LightSpeed.Model.TypeModel.InitializeValueFields(IEnumerable`1 fieldInfos)
Mindscape.LightSpeed.Model.TypeModel.InitializeFields(ICollection`1 associationModels)
Mindscape.LightSpeed.Model.TypeModel.LoadFields(IEnumerable`1 typeModels)
Mindscape.LightSpeed.Model.TypeModel.GetTypeModel(Type type)
..(Query , IList )
Mindscape.LightSpeed.UnitOfWork.Find(Query query, IList results)
Mindscape.LightSpeed.UnitOfWorkBase.Find(Query query)
Mindscape.LightSpeed.Linq.Plan.SingleQueryPlan.ExecuteImmediate(IUnitOfWork unitOfWork, Type returnType)
Mindscape.LightSpeed.Linq.LinqQueryProvider.Execute(Expression expression)
Mindscape.LightSpeed.Linq.LinqQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression)
Mindscape.LightSpeed.Linq.LinqQuery`1.GetEnumerator()
Cloud.Data.UnitOfWorkExtensions.AnyUser(IUnitOfWork unitOfWork, String username, String password)
I tried to rewrite it as follows:
var query = new Query(typeof(User))
{
QueryExpression = Entity.Attribute(User.UsernameField) == username
};
var users = unitOfWork.Find<User>(query);
And the exception is still a null reference exception, but the stack trace looks as follows:
Mindscape.LightSpeed.Model.TypeModel.FindValueModel(String name)
Mindscape.LightSpeed.Model.TypeModel.InitializeValueFields(IEnumerable`1 fieldInfos)
Mindscape.LightSpeed.Model.TypeModel.InitializeFields(ICollection`1 associationModels)
Mindscape.LightSpeed.Model.TypeModel.LoadFields(IEnumerable`1 typeModels)
Mindscape.LightSpeed.Model.TypeModel.GetTypeModel(Type type)
..(Query , IList )
Mindscape.LightSpeed.UnitOfWork.Find(Query query, IList results)
Mindscape.LightSpeed.UnitOfWorkBase.Find[TEntity](Query query)
Cloud.Data.UnitOfWorkExtensions.AnyUser(IUnitOfWork unitOfWork, String username, String password)
The users and its parent class looks as follows:
[Discriminator(Value = "User")]
public class User : Principal
{
[ValidateLength(0, 255)]
private string _username;
[ValidateLength(0, 255)]
private string _password;
[ReverseAssociation("User")]
private readonly EntityCollection<Authorization> _authorizations = new EntityCollection<Authorization>();
private long _personId;
[ReverseAssociation("Users")]
private readonly EntityHolder<Person> _person = new EntityHolder<Person>();
public const string UsernameField = "Username";
public const string PasswordField = "Password";
public string Username
{
get
{
return Get(ref _username, "Username");
}
set
{
Set(ref _username, value, "Username");
}
}
public string Password
{
get
{
return Get(ref _password, "Password");
}
set
{
Set(ref _password, value, "Password");
}
}
public EntityCollection<Authorization> Authorizations
{
get
{
return Get(_authorizations);
}
}
public long PersonId
{
get
{
return Get(ref _personId, "Person");
}
set
{
Set(ref _personId, value, "Person");
}
}
public Person Person
{
get
{
return Get(_person);
}
set
{
Set(_person, value);
}
}
protected override void OnSaving(CancelEventArgs e)
{
if (EntityState == EntityState.New)
{
var manager = ServiceLocator.Current.GetInstance<CryptographyManager>();
if (!string.IsNullOrWhiteSpace(Password))
{
Password = manager.CreateHash("Password", Password);
}
}
base.OnSaving(e);
}
}
[Discriminator(Attribute = "Discriminator")]
public class Principal : Entity<long>
{
[ValidateLength(0, 255)]
private string _discriminator;
[UsedImplicitly]
private int _lockVersion;
[UsedImplicitly]
private DateTime _createdOn;
[UsedImplicitly]
private DateTime _updatedOn;
[UsedImplicitly]
private DateTime? _deletedOn;
[ValidateLength(0, 255)]
private string _displayName;
[ReverseAssociation("Principal")]
private readonly EntityCollection<AccessRule> _accessRules = new EntityCollection<AccessRule>();
public const string DiscriminatorField = "Discriminator";
public const string DisplayNameField = "DisplayName";
public int LockVersion
{
get
{
return _lockVersion;
}
}
public DateTime CreatedOn
{
get
{
return _createdOn;
}
}
public DateTime UpdatedOn
{
get
{
return _updatedOn;
}
}
public DateTime? DeletedOn
{
get
{
return _deletedOn;
}
}
public string Discriminator
{
get
{
return Get(ref _discriminator, "Discriminator");
}
set
{
Set(ref _discriminator, value, "Discriminator");
}
}
public string DisplayName
{
get
{
return Get(ref _displayName, "DisplayName");
}
set
{
Set(ref _displayName, value, "DisplayName");
}
}
public EntityCollection<AccessRule> AccessRules
{
get
{
return Get(_accessRules);
}
}
}
I tried to isolate it to a separate solution, but I was unable to reproduce the bug.
Any ideas?
Thanks,
Werner