gpt4 book ai didi

c# - QueryOver 中的 SelectList

转载 作者:行者123 更新时间:2023-11-30 23:07:47 25 4
gpt4 key购买 nike

我在通过 QueryOver 选择时遇到一些问题,这是我的模型类:

public class BaseModel
{
public virtual int Id { get; set; }
public virtual DateTime ModifyDate { get; set; }
public virtual DateTime CreateDate { get; set; }
public virtual User CreateUser { get; set; }
public virtual User ModifyUser { get; set; }
public virtual bool IsActive { get; set; }
}

public class BaseClassMap<T> : ClassMap<T> where T : BaseModel
{
public BaseClassMap()
{
Id(x => x.Id).GeneratedBy.Identity();
References(x => x.CreateUser, "CreateUser_Id");
References(x => x.ModifyUser, "ModifyUser_Id");
Map(x => x.CreateDate).Not.Nullable().Default("GETDATE()");
Map(x => x.ModifyDate).Not.Nullable().Default("GETDATE()"); ;
Map(x => x.IsActive).Not.Nullable().Default("1");
}
}

public class Expenditure : BaseModel
{
public virtual decimal Amount { get; set; }
public virtual Category Category { get; set; }
public virtual string Comment { get; set; }
public virtual DateTime Month { get; set; }
}

public class ExpenditureMap : BaseClassMap<Expenditure>
{
public ExpenditureMap()
{
Schema("[budget]");
Table("[Expenditure]");
Id(x => x.Id);
Map(x => x.Amount).Not.Nullable();
Map(x => x.Comment);
Map(x => x.Month).Not.Nullable();
References(x => x.Category, "Category_Id").Not.Nullable();
}
}

public class User : BaseModel
{
public virtual string Login { get; set; }
public virtual string Email { get; set; }
public virtual string Password { get; set; }
public virtual string PasswordSalt { get; set; }
public virtual string HashAlgorithm { get; set; }
}

public class UserMap : BaseClassMap<User>
{
public UserMap()
{
Schema("[permission]");
Table("[User]");
Id(x => x.Id);
Map(x => x.Login).Not.Nullable();
Map(x => x.Email).Not.Nullable();
Map(x => x.Password).Not.Nullable();
Map(x => x.PasswordSalt);
Map(x => x.HashAlgorithm).Not.Nullable();
}
}

public class ExpenseDetailsItemVm
{
public int Id { get; set; }
public string Comment { get; set; }
public decimal Amount { get; set; }
public DateTime CreateDate { get; set; }
public string CreateUser { get; set; }
}

这是我的查询:

var user = new User();
var result = new ExpenseDetailsItemVm();
var expenses = SessionManager.Session.QueryOver<Expenditure>()
.JoinAlias(x => x.CreateUser, () => user)
.Where(x => x.IsActive &&
x.Category.Id == categoryId &&
x.Month == date.ToFirstDayOfMonth())
.SelectList(list => list
.Select(x => x.Id).WithAlias(() => result.Id)
.Select(x => x.Amount).WithAlias(() => result.Amount)
.Select(x => x.Comment).WithAlias(() => result.Comment)
.Select(x => x.CreateDate).WithAlias(() => result.CreateDate)
.Select(()=> user.Login).WithAlias(() => result.CreateUser)
).TransformUsing(Transformers.AliasToBean<ExpenseDetailsItemVm>())
.List<ExpenseDetailsItemVm>();

我收到这个错误:

Object reference not set to an instance of an object.

我确定问题出在这行代码上:

 .Select(()=> user.Login).WithAlias(() => result.CreateUser)

如果我注释掉这一行,查询工作正常并返回结果没有错误。

为了消除映射错误,我尝试了这个:

var u = SessionManager.Session.QueryOver<User>().Where(x => x.Id == 1).
SelectList(list => list.
Select(x => x.Login).WithAlias(() => result.CreateUser))
.TransformUsing(Transformers.AliasToBean<ExpenseDetailsItemVm>())
.List<ExpenseDetailsItemVm>();

这也很好用。

在我的Expenditure 表中,列CreateUser_Id 各处都有一个值

最佳答案

尝试将用作别名的对象作为null:

User user = null; 
ExpenseDetailsItemVm result = null;

因为它们仅用于创建具有强类型语法的 SQL。

关于c# - QueryOver 中的 SelectList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46981309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com