1

我正在尝试查询 nhibernate 和 MSSQL 中的简单数据结构

dbo.Projects : Id(int, not null)

dbo.Finances : Id(int, not null), ProjectId(int,not null), dbo.projects 的外键引用

我想获取项目表中不存在于财务表中的所有记录,其中财务表具有外键引用 ProjectId。我要从 EntityFramework 迁移到 (Fluent) Nhibernate 3?

    //So far I have got here: 
    public IQueryable<ProjectModel> GetProjectsNotPresentInFinance()
    {
        var factory = Fluently.Configure()
            .Database(MsSqlConfiguration
                .MsSql2008
                    .ConnectionString(m_connectionString))
            .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<ProjectMap>()                   
               ).BuildSessionFactory();

        using (var session = factory.OpenSession())
        {
            var allprojects = session.QueryOver<ProjectModel>();
            var projectsToReturn = allprojects.List<ProjectModel>().AsQueryable();

       //---    Something like : all the records not in finances table ---------
       // .Where( proj => !db.Finances.Where(fin => fin.ProjectId == proj.Id).Any())
       // .Select(project => new ProjectModel
       // {
       //     Id=project.Id,
       //     ProjectName = project.ProjectName,                 
       // });

            return projectsToReturn;
        }            

    }



public class FinanceModel
{
    public virtual int Id { get; set; }
    public virtual int ProjectId { get; set; }       
}

public class ProjectModel
{
    public virtual int Id { get; set; }
    public virtual string ProjectName { get; set; }
}

public class ProjectMap:ClassMap<ProjectModel>
{
    public ProjectMap() {
        Table("Projects");
        Id(x => x.Id);
        Map(x => x.ProjectName);
    }
}

public class FinanceMap : ClassMap<FinanceModel>
{
    public FinanceMap()
    {
        Table("Finances");
        Id(x => x.Id);
        References(x => x.ProjectModel);
    }
}

    //-------------------------------------------------------
    //This is an Equivalent working code Using EntityFramework :
    public IQueryable<ProjectModel> GetProjectsNotPresentInFinance() {
        IQueryable<ProjectModel> projectList = db.Projects
        .Where( proj => !db.Finances.Where(fin => fin.ProjectId == proj.Id).Any())
        .Select(project => new ProjectModel
        {
            Id=project.Id,
            ProjectName = project.ProjectName,                 
        });
        return projectList;
    }
    //-------------------------------------------------------
4

1 回答 1

1

再三考虑,您可以在不更改任何映射的情况下尝试使用子查询:

var notOrphanProjectIdsSubquery = QueryOver.Of<FinanceModel>()
                                    .Select(x => x.ProjectId);

var orphanProjects =  session.QueryOver<ProjectModel>()
                         .WithSubquery
                         .WhereProperty(x=>x.Id)
                         .NotIn(notOrphanProjectIdsSubquery)
                         .List();

------------------------初步回答

假设您的班级中有一个映射的Finances属性Project,并且根据https://stackoverflow.com/a/14980450/1236044,它应该类似于:

var orphanProjects = session.QueryOver<ProjectModel>()
    .WhereRestrictionOn(x => x.Finances).IsEmpty()
    .List();

我必须承认我不精通 FluentNH。我猜类和映射应该是这样的,希望我不会让你走错路……

public class FinanceModel
{
    public virtual int Id { get; set; }
    public virtual int ProjectId { get; set; }       
    public virtual ProjectModel Project{get;set;}
}

public class ProjectModel
{
    public virtual int Id { get; set; }
    public virtual string ProjectName { get; set; }
    public virtual IList<FinanceModel> Finances { get; set; }
}


public class ProjectMap:ClassMap<ProjectModel>
{
    public ProjectMap() {
        Table("Projects");
        Id(x => x.Id);
        Map(x => x.ProjectName);
        HasMany(x => x.Finances);
    }
}

public class FinanceMap : ClassMap<FinanceModel>
{
    public FinanceMap()
    {
        Table("Finances");
        Id(x => x.Id);
        References(x => x.Project);
    }
}
于 2013-02-25T12:33:16.137 回答