Daniel Williams
Daniel Williams

Reputation: 9304

Getting multiple results in almost identical queries

I have two very similar queries in FNH, acting on similar classes, Project and Foo. Foo is simply a new local class derived from Project. Its classMap is derived from Project's classMap as well.

    public class Foo:pcm.Domain.Project { }
    public class FooMap : pcm.Mappings.ProjectMapBase<Foo> { }

Here are my queries, using the same session:

        var list = session.Query<Project>()
            .Take(10)
            .ToList();

        var list2 = session.Query<foo>()
            .Take(10)
            .ToList();

        Assert.Equal(list.Count(),list2.Count());

The assertion fails because list has 30 elements, and list2 has 10. There are indeed only 10 projects.

Edit: Here are the mappings for the base, which sits in its own DLL.

public class ProjectBaseMapTemplate<T> : ClassMap<T> where T: ProjectBase
{
    public ProjectBaseMapTemplate()
    {
        Table("proj");
        Id(x => x.MasterKey, "master_key");
        Map(x => x.ProjectName).Column("project_name");
        Map(x => x.ProjectTitle, "project_title");
    }
}
public class ProjectBaseMap : ProjectBaseMapTemplate<ProjectBase> { }

Here are the derived class, which sit in another DLL. This separation of classes into DLLs is fundamentally important to how I need to use FNH.

    public class Foo:pcm.Domain.ProjectBase 
    {
            public virtual int ProjectState{ get; set; }
    }
    public class FooMap : pcm.Mappings.ProjectBaseMapTemplate<Foo> 
    {
        public FooMap()
        {
            Map(x=> x.ProjectState).Column("project_state");
        }
    }

Update Here is the ProjectBase

public class ProjectBase
{
    public virtual string MasterKey { get; set; }
    public virtual string ProjectName { get; set; }
    public virtual string ProjectTitle { get; set; }
}

Upvotes: 1

Views: 77

Answers (2)

Cole W
Cole W

Reputation: 15303

It could be related to this:

(10) polymorphism (optional, defaults to implicit): Determines whether implicit or explicit query polymorphism is used.

Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or the class and that instances of any subclass of the class will be returned by a query that names the class itself. Explicit polymorphism means that class instances will be returned only be queries that explicitly name that class and that queries that name the class will return only instances of subclasses mapped inside this declaration as a or . For most purposes the default, polymorphism="implicit", is appropriate. Explicit polymorphism is useful when two different classes are mapped to the same table (this allows a "lightweight" class that contains a subset of the table columns).

So you would fix your mappings by using: Polymorphism.Explicit();

Ex.

public class FooMap : pcm.Mappings.ProjectBaseMapTemplate<Foo> 
{
    public FooMap()
    {
        Polymorphism.Explicit();
        Map(x=> x.ProjectState).Column("project_state");
    }
}

Upvotes: 0

Mohsen Heydari
Mohsen Heydari

Reputation: 7284

When you use Take(10) function The Underlying select all query will be run based on your RDBMS functionality
Then top 10 result will be returned (paging)

Since you don't use an ORDER BY expression, same queries will result different in multiple executions due to paging and RDBMS functionality.

Hope be useful.

Upvotes: 1

Related Questions