基本上我正在使用 NopCommerce 3.2 。我修改了存储过程,因此它可以返回与CategoryName
详细信息关联的Prodcut
,例如;
--return products
SELECT TOP (@RowsToReturn)
p.*, c.Name as CategoryName
FROM
#PageIndex [pi]
INNER JOIN Product p with (NOLOCK) on p.Id = [pi].[ProductId]
-- Custom INNER JOIN To Get CategoryName
INNER JOIN Product_Category_Mapping pc ON pc.ProductId = p.Id
INNER JOIN Category c ON c.Id = pc.CategoryId
WHERE
[pi].IndexId > @PageLowerBound AND
[pi].IndexId < @PageUpperBound
ORDER BY
[pi].IndexId
正如您所见ResultSet
,CategoryName
已被检索;
在我的代码中,我创建了一个带有ignor映射的自定义属性;
// Product.cs
public string CategoryName { get; set; }
// another mapping class i.e ProductMapp.cs
this.Ignore(p => p.CategoryName);
使用此代码(minimezed)检索实际的ResultSet
;
// some parameters sent by nopcommerce
var pTotalRecords = _dataProvider.GetParameter();
pTotalRecords.ParameterName = "TotalRecords";
pTotalRecords.Direction = ParameterDirection.Output;
pTotalRecords.DbType = DbType.Int32;
.........
//invoke stored procedure
var products = _dbContext.ExecuteStoredProcedureList<Product>(
"ProductLoadAllPaged",
.............
.............
pTotalRecords);
方法(ExecuteStoredProcedureList)定义在这里;
注意:方法代码太长而无法粘贴,因此我尝试仅粘贴执行和映射代码
//var connection = context.Connection;
var connection = this.Database.Connection;
//Don't close the connection after command execution
//open the connection for use
if (connection.State == ConnectionState.Closed)
connection.Open();
//create a command object
using (var cmd = connection.CreateCommand())
{
//command to execute
cmd.CommandText = commandText;
cmd.CommandType = CommandType.StoredProcedure;
// move parameters to command object
if (parameters != null)
foreach (var p in parameters)
cmd.Parameters.Add(p);
//database call
var reader = cmd.ExecuteReader();
//return reader.DataReaderToObjectList<TEntity>();
var result = context.Translate<TEntity>(reader).ToList();
for (int i = 0; i < result.Count; i++)
result[i] = AttachEntityToContext(result[i]);
//close up the reader, we're done saving results
reader.Close();
return result;
}
如您所见,它正在映射到Product
实体。但是,在我的结果集中,coloumn无法自动映射,它始终为null。我想将CategoryName
coloumn值映射到CategoryName
属性。我怎么能做到这一点?
答案 0 :(得分:0)
您可以使用必填字段在Nop.Core
中创建另一个类,而返回的产品表则返回该表。这个类可以是临时的,通过在存储过程本身创建表并在最后删除。