NHibernate QueryOver, could not resolve property 'Foobar' - fluent-nhibernate

public class Base {
public virtual bool Foobar {get;set;}
}
public class MyClass : Base {
public virtual string Foo {get;set;}
public virtual DateTime Bar {get;set;}
}
var query = Session.QueryOver<MyClass>()
.Where(mc => mc.Foo == "Foo" || mc.Bar >= DateTime.Now() && mc.Foobar == false).ToRowCountQuery().Future<int>();
Anyone help with the error I'm getting, "could not resolve property 'Foobar'"
This query is one of several that will execute thus the Future usage.

Related

Null check for where clause in EF

I have a POCO entity that looks like
public class Rebate : IEntity
{
[Key]
public int Id { get; set; }
[ForeignKey("ClassOneId")]
public virtual ClassOne ClassOne { get; set; }
public int ClassOneId { get; set; }
[ForeignKey("ClassTwoCode")]
public virtual ClassTwo ClassTwo { get; set; }
public string ClassTwoCode { get; set; }
public double Rebate { get; set; }
}
If ClassOne has a value ClassTwo will be null and vice versa, now when I want to run a query checking the name property of each class using linqkit:
var predicate = PredicateBuilder.True<Rebate>();
if (!string.IsNullOrEmpty(term))
predicate = predicate.And(x => (x.ClassOne != null ?
x.ClassOne.Name.Contains(term) : x.ClassTwo.Name.Contains(term)));
OR
var predicate = PredicateBuilder.False<Rebate>();
if (!string.IsNullOrEmpty(term))
predicate = predicate.Or(x => x.ClassOne.Name.Contains(term)).
Or(x.ClassTwo.Name.Contains(term)));
In both cases I get results where ClassOne matches the term but no results where ClassTwo matches.
My understanding of what is happening is that when ClassOne is null the query is failing and not checking the name property of ClassTwo. What I'm not sure of is how to work around this without running the queries separately and merging the results.
Any one have a better plan?
Does this work?
x => (x.ClassOne != null && x.ClassOne.Name.Contains(term)) ||
(x.ClassTwo != null && x.ClassTwo.Name.Contains(term))
I know EF handles null checks differently than it would if you were working with in-memory objects because of how SQL needs to handle them. If this doesn't work, try running your query in LINQPad and check what SQL is getting executed and check if it makes sense.

NHibernate: cannot resolve inherited id property

I have the entity defined below:
public class Foo : Entity<Foo.FooId>
{
public class FooId
{
public virtual String Bar { get; protected internal set; }
public virtual Int32 Buzz { get; protected internal set; }
}
// ...
}
And here's the base class:
public abstract class Entity<T> : IEquatable<Entity<T>>
{
public virtual T Id { get; protected internal set; }
// ...
}
I'm going to map the "Id" property as a "composite key", so I've added the following mapping class:
public class FooMap : ClassMapping<Foo>
{
public FooMap()
{
ComponentAsId(x => x.Id, m =>
{
m.Property(p => p.Bar);
m.Property(p => p.Buzz);
});
}
}
And that's all pretty nice, but I get an error with the following querying attempt:
session.QueryOver<Foo>()
.Where(m => m.Id.Bar == "a" &&
m.Id.Buzz == 2).List();
The error I get is:
NHibernate.QueryException : could not resolve property: Id of: Foo
It's quite strange, because by removing the base class and encapsulating everything within "Foo", it works like a charm.
Thanks in advance.
This was a bug and reported as NH-3105. It is now fixed in the most recent of the source code and will be released as 3.3.3.GA.

create TypeModel at runtime using Protobuf-Net, Unexpected sub-type

I have the class structure below and would like to serialize it at runtime using Protobuf-Net. Unfortunately I get error "Unexpected sub-type: Web2Pdf". Why?
var web2PdfEntity = new Web2Pdf();
web2PdfEntity.Property1 = 1;
web2PdfEntity.Property2 = 2;
web2PdfEntity.Property3 = 3;
var model = TypeModel.Create();
model.Add(typeof (EntityBase), true).AddSubType(20000, typeof (WebEntity)).AddSubType(30000,typeof (Web2Pdf));
model.CompileInPlace();
using (var stream = new FileStream(#"C:\1.txt", FileMode.Create, FileAccess.Write, FileShare.None))
{
model.Serialize(stream, web2PdfEntity); //Get exception here!
}
[ProtoContract]
public abstract class EntityBase
{
[ProtoMember(1011)]
public int Property1 { get; set; }
}
[ProtoContract]
public abstract class WebEntity : EntityBase
{
[ProtoMember(1012)]
public int Property2 { get; set; }
}
[ProtoContract]
public sealed class Web2Pdf : WebEntity
{
[ProtoMember(1013)]
public int Property3 { get; set; }
}
The subtypes must be associated with the immediate parent, so: EntityBase needs to know about WebEntity, and WebEntity needs to know about Web2Pdf (rather than EntityBase knowing about both and WebEntity not knowing about Web2Pdf).
For info, smaller tag numbers are more efficient, too - but up to you.
Additionally, this can all be done via [ProtoInclude(...)], which can be more convenient if the sub-type numbers are fixed.

Nhibernate projection with child collection

Using NHibernate 2.1, I'm trying to project an entity and its child collection into a DTO. My entity looks like this..
public class Application
{
public int Id {get;set;}
public string Name {get;set;}
public List<ApplicationSetting> Settings {get;set;}
// A bunch of other properties that I don't want in the DTO
}
public class ApplicationSetting
{
public int Id {get;set;}
public string Name {get;set;}
public string Code {get;set;}
// A bunch of other properties that I don't want in the DTO
}
My DTO looks like this..
public ApplicationDto
{
public int Id {get;set;}
public string Name {get;set;}
public List<ApplicationSettingDto> Settings {get;set;}
}
public class ApplicationSettingDto
{
public int Id {get;set;}
public string Name {get;set;}
public string Code {get;set;}
}
My code to select JUST the Application and project it is this (using Nhibernate 2.1 and nhLambdaExtensions)
var applicationAlias = new Application();
var criteria = Session
.Add<Application>(a => a.Id == id);
int? Id = null;
string Name = null;
criteria
.SetProjection
(
Projections.Distinct(
Projections.ProjectionList()
.Add(LambdaProjection.Property<Application>(a => a.Id).As(() => Id))
.Add(LambdaProjection.Property<Application>(a => a.Name).As(() => Name))
)
);
criteria.SetResultTransformer(Transformers.AliasToBean(typeof(ApplicationDto)));
var contract = criteria.UniqueResult<ApplicationDto>();
My question is, how do I project just SOME of the properties from the ApplicationSettings entity to the ApplicationSettingsDto child collection?
I think you might need to do a MutiQuery and bring together the DTO parents and children yourself.

ICriteria subquery

I have to simple entities
public class EntityA
{
public virtual int ID { get;set;}
public virtual string Name { get;set;}
public virtual IList<EntityB> BList { get;set;}
public EntityA()
{
BLIst = new List<EntityB>();
}
}
public class EntityB
{
public virtual int ID { get;set;}
public virtual string Name { get;set;}
public virtual int Value { get;set;}
public virtual EntityA EntityA { get;set;}
}
How do i make a ICriteria Query where EntityA.Name = 'SearchString' and List should be queried (((EntityB.Name='Name1' And (EntityB.Value=1)) And ((EntityB.Name='Name2') And (EntityB.Value=1)))
The search can contain a List and i tried the following query:
if (SearchBLIst.Count > 0)
{
foreach (EntityB searchAttribute in SearchBLIst)
{
Junction disjunction1 = Restrictions.Disjunction();
disjunction1.Add(
Expression.Eq("entityB.ID", searchAttribute.ID) &&
Expression.Ge("attributeValues.Value",searchAttribute.value));
store.Add(disjunction1);
}
}
Entity b name is expected to be 2 different values at the same time.
I'm guessing you want to use the other entity's property in the restriction too. You need "join alias".