小编meh*_*cek的帖子

NHibernate:如何通过一个没有lazyload的sql查询,通过子实体上的过滤器来获取子实体?

我使用NHibernate 3.3.1和FluentNhibernate 1.3进行数据层.

我有以下实体:

在此输入图像描述

数据库图: 在此输入图像描述

我需要一种方法来获取产品媒体的MediaCategory的产品.我希望NHibernate只向db发送一个查询并获取产品的所有子属性.

我希望NHibernate发送这样的查询:

declare @mediaCategoryId int = 13
select * 
from Product p 
inner join Media m on m.ProductId=p.Id 
inner join MediaCategoryMedia mcm on mcm.MediaId=m.Id 
inner join MediaCategory mc on mc.Id=mcm.MediaCategoryId 
left join ProductSeller ps on ps.ProductId=p.Id 
left join Seller s on ps.SellerId=s.Id 
where mc.Id=@mediaCategoryId 
Run Code Online (Sandbox Code Playgroud)

我尝试了以下选项来解决这一挑战;

  1. session .QueryOver< ProductEntity >()...
    我试过Inner.JoinQueryOver< .. >().Fetch.Eager......但我无法获取所有子实体.

  2. session.CreateCriteria< ProductEntity >().SetFetchMode("",FetchMode.Eager)...
    在这种情况下,延迟加载工作,我不想要lazyload.如果我从映射中禁用lazyload,NH会发送大量查询..我想要的是一个单一查询来获取所有子实体.

  3. session.Query< ProductEntity >().FetchMany(p=>p.MediaList).ThenFetchMany(m=>m.SellerList)...
    在这种情况下,我无法创建传递mediaCategoryId过滤器的别名.相反,我使用了.Where(x=>x.MediaList.Any(m=>m.CategoryList.Any(...))),生成的查询也不是最佳的.

  4. (来自p中的
    会话.查询<ProductEntity>()from m in p.MediaList
    from c in …

c# nhibernate hql fluent-nhibernate c#-4.0

6
推荐指数
1
解决办法
2万
查看次数

WebApi2:自定义参数绑定以绑定部分参数

我有一个webApi2项目和另一个项目,其中我有我的Model类和BaseModel,它是所有模型的基础,如下所示,

public class BaseModel
{
    public string UserId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

所有其他模型都是从我的BaseModel派生的.

在webapi我有我的CustomerController如下,

public class CustomerController : ApiController
{
    [HttpPost]
    public GetCustomerResponseModel Get(GetCustomerRequestModel requestModel)
    {
        var response = new GetCustomerResponseModel();

        //I need only the UserId coming from the BaseModel is binded from request headers
        var userId = requestModel.UserId;

        //I want all model data except UserId is binded with default model binding
        var customerData = requestModel.CustomerData;
        var someOtherData = requestModel.SomeOtherData;

        return response;
    }

    [HttpPost]
    public AddStockAlertResponseModel AddStockAlert(AddStockAlertRequestModel requestModel)
    { …
Run Code Online (Sandbox Code Playgroud)

model-binding parameterbinding custom-model-binder asp.net-web-api asp.net-web-api2

6
推荐指数
1
解决办法
2377
查看次数