ฉันกำลังพยายามรับค่าสูงสุดของเขตข้อมูลจำนวนเต็มในตาราง โดยเฉพาะอย่างยิ่ง ฉันกำลังพยายามเพิ่มฟิลด์ "InvoiceNumber" โดยอัตโนมัติเมื่อเพิ่มใบแจ้งหนี้ใหม่ ฉันไม่ต้องการให้สิ่งนี้เป็นฟิลด์การเพิ่มอัตโนมัติในฐานข้อมูล แต่เนื่องจากมันถูกควบคุมโดยผู้ใช้ - ฉันแค่พยายามดูแลกรณีเริ่มต้น ตอนนี้ผมใช้อยู่
session.CreateCriteria<Invoice>()
.SetProjection(Projections.Max("InvoiceNumber"))
.FutureValue<int>();
เพื่อให้ได้หมายเลขใบแจ้งหนี้ที่ใหญ่ที่สุดในฐานข้อมูลอยู่แล้ว วิธีนี้ใช้งานได้ดี ยกเว้นเมื่อไม่มีใบแจ้งหนี้ในฐานข้อมูลอยู่แล้ว จากนั้นฉันจะได้รับ System.ArgumentException: ค่า "" ไม่ใช่ประเภท "System.Int32" และไม่สามารถใช้ในคอลเลกชันทั่วไปนี้ได้ การเปลี่ยนไปใช้ FutureValue<int?>()
ไม่สามารถแก้ปัญหาได้ มีวิธีบอก NHibernate ให้แมปสตริงว่างเป็นโมฆะหรือไม่? หรือมีวิธีที่ดีกว่าในการบรรลุเป้าหมายของฉันโดยสิ้นเชิง?
การติดตามสแต็กของข้อยกเว้น (อย่างน้อยก็ส่วนที่เกี่ยวข้อง) คือ
NHibernate.HibernateException: Error executing multi criteria : [SELECT max(this_.[InvoiceNumber]) as y0_ FROM dbo.[tblInvoice] this_;
SELECT this_.ID as ID647_0_, this_.[NHVersion] as column2_647_0_, this_.[Description] as column3_647_0_, this_.[DiscountPercent] as column4_647_0_, this_.[DiscountDateDays] as column5_647_0_, this_.[PaymentDueDateDays] as column6_647_0_, this_.[Notes] as column7_647_0_, this_.[DiscountDateMonths] as column8_647_0_, this_.[PaymentDueDateMonths] as column9_647_0_, this_.[DiscountDatePeriod] as column10_647_0_, this_.[DiscountDateMonthlyDay] as column11_647_0_, this_.[DiscountDateMonthlyDayDay] as column12_647_0_, this_.[DiscountDateMonthlyDayMonth] as column13_647_0_, this_.[DiscountDateMonthlyThe] as column14_647_0_, this_.[DiscountDateMonthlyTheDOW] as column15_647_0_, this_.[DiscountDateMonthlyTheMonth] as column16_647_0_, this_.[DiscountDateMonthlyTheWeek] as column17_647_0_, this_.[PaymentDueDatePeriod] as column18_647_0_, this_.[PaymentDueDateMonthlyDay] as column19_647_0_, this_.[PaymentDueDateMonthlyDayDay] as column20_647_0_, this_.[PaymentDueDateMonthlyDayMonth] as column21_647_0_, this_.[PaymentDueDateMonthlyThe] as column22_647_0_, this_.[PaymentDueDateMonthlyTheDOW] as column23_647_0_, this_.[PaymentDueDateMonthlyTheMonth] as column24_647_0_, this_.[PaymentDueDateMonthlyTheWeek] as column25_647_0_ FROM dbo.[tblTermsCode] this_;
] ---> System.ArgumentException: The value "" is not of type "System.Int32" and cannot be used in this generic collection.
Parameter name: value
at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value, Type targetType)
at System.Collections.Generic.List`1.VerifyValueType(Object value)
at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item)
at NHibernate.Impl.MultiCriteriaImpl.GetResultsFromDatabase(IList results)