RavenDB转换 - 转换Id

时间:2017-04-25 11:11:47

标签: c# ravendb

我正在尝试运行RavenDB转换,但我遇到了以下异常:

  

Raven.Imports.Newtonsoft.Json.JsonSerializationException:无法读取属性值:Id ---> System.FormatException:输入字符串的格式不正确。

我认为这是因为我的模型使用整数ID而不是字符串。如何让转换与int一起使用?

DiscountCode_ViewModel.cs

public class DiscountCode_ViewModel : AbstractTransformerCreationTask<DiscountCode>
{
    public DiscountCode_ViewModel()
    {
        TransformResults = discounts => discounts
            .Select(discount => new SimpleDiscountCode
            {
                // Throws an exception here
                Id = discount.Id,
                MerchantId = discount.MerchantId,
                TermsAndConditions = discount.TermsAndConditions,
                Code = discount.Code,
                Description = discount.Description
            });
    }
}

DiscountCode.cs

public class DiscountCode
{
    public int Id { get; set; }
    public int MerchantId { get; set; }
    public string Description { get; set; }
    public string Code { get; set; }
    public string TermsAndConditions { get; set; }

    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int SortOrder { get; set; }

    // a bunch more properties...
}

SimpleDiscountCode.cs

public class SimpleDiscountCode
{
    public int Id { get; set; }
    public int MerchantId { get; set; }
    public string Title { get; set; }
    public string Code { get; set; }
    public string TermsAndConditions { get; set; }
}

DiscountCode_ByMerchantId.cs

public class DiscountCodes_ByMerchantId : AbstractIndexCreationTask<DiscountCode>
{
    public DiscountCodes_ByMerchantId()
    {
        Map = discounts => discounts
            .Select(discount => new
            {
                discount.MerchantId,
                discount.StartDate,
                discount.EndDate,
                discount.SortOrder
            });

        Sort(x => x.SortOrder, SortOptions.Int);
    }
}

Query.cs

var discounts = await session
                .Query<DiscountCode, DiscountCodes_ByMerchantId>()
                .Include<DiscountCode, Merchant>(d => d.MerchantId)
                .Where(x => x.MerchantId == id)
                .Where(x => x.StartDate <= DateTime.Now && x.EndDate >= DateTime.Now)
                .OrderBy(x => x.SortOrder)
                .TransformWith<DiscountCode_ViewModel, SimpleDiscountCode>()
                .ToListAsync();

var merchant = await session.LoadAsync<Merchant>(id);

堆栈跟踪

  

发生了Raven.Imports.Newtonsoft.Json.JsonSerializationException     的HResult = -2146233088     Message =无法读取属性值:Id     来源= Raven.Abstractions     堆栈跟踪:          at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String id)在C:\ Builds \ RavenDB-Stable-3.5 \ Imports \ Newtonsoft.Json \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs:2015年          在Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader,Type objectType,JsonContract contract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerMember,Object existingValue)中的C:\ Builds \ RavenDB-Stable-3.5 \ Imports \ Newtonsoft.Json \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs:第434行          在Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader,Type objectType,Boolean checkAdditionalContent)C:\ Builds \ RavenDB-Stable-3.5 \ Imports \ Newtonsoft.Json \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs:第180行          在Raven.Imports.Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type objectType)在C:\ Builds \ RavenDB-Stable-3.5 \ Imports \ Newtonsoft.Json \ Src \ Newtonsoft.Json \ JsonSerializer.cs:第732行          at Raven.Client.Document.SessionOperations.QueryOperation.DeserializedResult [T](RavenJObject result)在C:\ Builds \ RavenDB-Stable-3.5 \ Raven.Client.Lightweight \ Document \ SessionOperations \ QueryOperation.cs:第262行          at Raven.Client.Document.SessionOperations.QueryOperation.Deserialize [T](RavenJObject result)在C:\ Builds \ RavenDB-Stable-3.5 \ Raven.Client.Lightweight \ Document \ SessionOperations \ QueryOperation.cs:第190行          at Raven.Client.Document.SessionOperations.QueryOperation.b__26_0 [T](RavenJObject x)在C:\ Builds \ RavenDB-Stable-3.5 \ Raven.Client.Lightweight \ Document \ SessionOperations \ QueryOperation.cs:第139行          在System.Linq.Enumerable.SelectListIterator 2.ToList() at Raven.Client.Document.SessionOperations.QueryOperation.Complete[T]() in C:\Builds\RavenDB-Stable-3.5\Raven.Client.Lightweight\Document\SessionOperations\QueryOperation.cs:line 138 at Raven.Client.Document.AsyncDocumentQuery 1.d__118.MoveNext()在C:\ Builds \ RavenDB-Stable-3.5 \ Raven.Client.Lightweight \ Document \ AsyncDocumentQuery.cs:line 1230       ---从抛出异常的先前位置开始的堆栈跟踪结束---          在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)          在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)          在Raven.Client.Document.AsyncDocumentQuery 1.<ToListAsync>d__110.MoveNext() in C:\Builds\RavenDB-Stable-3.5\Raven.Client.Lightweight\Document\AsyncDocumentQuery.cs:line 1157 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()          在C:\ GitCode \ TCB.MerchantService \ src \ TCB.MerchantService.Data \ MerchantRepository.cs中的TCB.MerchantService.Data.MerchantRepository.d__3.MoveNext():第64行     的InnerException:          的HResult = -2146233033          消息=输入字符串格式不正确。          来源= System.Private.CoreLib          堆栈跟踪:               at System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer&amp; number,NumberFormatInfo info,Boolean parseDecimal)               在System.Number.ParseInt32(String s,NumberStyles样式,NumberFormatInfo信息)               在System.String.System.IConvertible.ToInt32(IFormatProvider提供程序)               at System.Convert.ChangeType(Object value,Type conversionType,IFormatProvider provider)               在Raven.Abstractions.Json.JsonNumericConverter`1.ReadJson(JsonReader reader,Type objectType,Object existingValue,JsonSerializer serializer)中的C:\ Builds \ RavenDB-Stable-3.5 \ Raven.Abstractions \ Json \ JsonNumericConverter.cs:第36行               在Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter转换器,JsonReader reader,Type objectType,Object existingValue)中的C:\ Builds \ RavenDB-Stable-3.5 \ Imports \ Newtonsoft.Json \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs:第1769行               在Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty属性,JsonConverter propertyConverter,JsonContainerContract containerContract,JsonProperty containerProperty,JsonReader reader,Object target)中的C:\ Builds \ RavenDB-Stable-3.5 \ Imports \ Newtonsoft.Json \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs:第900行               at Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader,JsonObjectContract contract,JsonProperty member,String id)在C:\ Builds \ RavenDB-Stable-3.5 \ Imports \ Newtonsoft.Json \ Src \ Newtonsoft.Json \ Serialization \ JsonSerializerInternalReader.cs:2005年          InnerException:

提琴手请求

  

POST /查询HTTP / 1.1   主持人:localhost:8080   连接:保持活力   内容长度:16   接受:application / json,text / javascript, / ; Q = 0.01   来源:http://localhost:8080   X-Requested-With:XMLHttpRequest   User-Agent:Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,类似Gecko)Chrome / 57.0.2987.133 Safari / 537.36   Content-Type:application / json;字符集= UTF-8   推荐人:http://localhost:8080/studio/index.html   Accept-Encoding:gzip,deflate,br   Accept-Language:en-US,en; q = 0.8   Cookie:_ga = GA1.1.1952671077.1488191167

0 个答案:

没有答案