gpt4 book ai didi

c# - Dapper 需要一个无参数的构造函数?

转载 作者:太空宇宙 更新时间:2023-11-03 17:06:43 24 4
gpt4 key购买 nike

我有一个类:

public class NListingsData : ListingData, IListingData
{
private readonly IMetaDictionaryRepository _metaDictionary;

//Constructor.
public NListingsData(IMetaDictionaryRepository metaDictionary)
{
_metaDictionary = metaDictionary;
}

//Overridden function from abstract base class
public override List<Images> Photos
{
get
{
var urlFormat = _metaDictionary.GetDictionary(CommonConstants.ImagesUrlFormat, this.Key);
var imgs = new List<Images>();

for (var i = 0; i < PhotosCount; i++)
{
imgs.Add(new Images
{
Url = string.Format(urlFormat, this.MNumber, i)

});
}
return imgs;
}
set { }
}
}

metaDictionary 由 Autofac 注入(inject)。

我正在使用 Dapper 执行查询,并尝试具体化 NListingsData。这是我正在使用的:

string sqlQuery = GetQuery(predicates); //Select count(*) from LView; select * from lView;
//Use multiple queries
using (var multi = _db.QueryMultipleAsync(sqlQuery,
new
{
//The parameter names below have to be same as column names and same as the fields names in the function: GetListingsSqlFilterCriteria()
@BedroomsTotal = predicates.GetBedrooms(),
@BathroomsTotalInteger = predicates.GetBathrooms()
}).Result)
{
//Get count of results that match the query
totalResultsCount = multi.ReadAsync<int>().Result.Single();
//Retrieve only the pagesize number of results

var dblistings = multi.ReadAsync<NListingsData>().Result; // Error is here
}
return dblistings;

我得到了错误

A parameterless default constructor or one matching signature (System.Guid ListingId, System.String MLSNumber, System.Int32 BedroomsTotal, System.Double BathroomsTotalInteger) is required for CTP.ApiModels.NListingsData materialization

我用来实现 dapper 的类是否必须始终是无参数的?

现在,我可以创建一个简单的 DataModel,然后映射到我的 ViewModel。但这是唯一的方法吗?

最佳答案

只需添加一个额外的私有(private)、无参数的构造函数。这将由 Dapper 挑选。

关于c# - Dapper 需要一个无参数的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374712/

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