NHibernate Projections.Max ทำงานกับตารางว่างอย่างไร

ฉันกำลังพยายามรับค่าสูงสุดของเขตข้อมูลจำนวนเต็มในตาราง โดยเฉพาะอย่างยิ่ง ฉันกำลังพยายามเพิ่มฟิลด์ "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)

person Neil    schedule 14.07.2010    source แหล่งที่มา


คำตอบ (2)


ใช้....UniqueValue<int?>();

NH ใช้ IList ที่ไม่ใช่แบบทั่วไปในการใช้งาน MultiCriteria ซึ่งใช้สำหรับการแบทช์ FutureValue ดูที่นี่เพื่อดูสาเหตุ List<int?> ไม่สามารถเพิ่ม null ผ่านการใช้งาน IList ฉันแปลกใจที่ฉันไม่เคยเจอเรื่องนี้มาก่อน หลีกเลี่ยงการใช้ประเภทค่าที่เป็นโมฆะกับ Future หรือ MultiCriteria

person dotjoe    schedule 14.07.2010
comment
FutureValue‹int?› มีปัญหาเดียวกัน ฉันตั้งใจจะเพิ่มสิ่งนั้นในคำถาม แต่ลืมใส่ไว้ในช่วงโค้ด ดังนั้นคำทั่วไปจึงหายไป - ขออภัย! มีความคิดอื่นอีกไหม? - person Neil; 15.07.2010
comment
แปลกมาก คุณใช้ฐานข้อมูลไหน? InvoiceNumber เป็นข้อมูลประเภทใด - person dotjoe; 15.07.2010
comment
นอกจากนี้ข้อยกเว้นนี้ถูกส่งไปที่ไหนกันแน่? คุณใช้ IList ที่ไม่ใช่แบบทั่วไปหรือไม่? - person dotjoe; 15.07.2010
comment
ฉันใช้ SQL Server (โดยเฉพาะปี 2005 แต่มันเกิดขึ้นในปี 2000 และ (ฉันคิดว่า) 2008 เช่นกัน) InvoiceNumber เป็น int ฉันได้เพิ่มข้อยกเว้นให้กับคำถาม หากนั่นช่วยได้ - person Neil; 16.07.2010

ด้วย QueryOver API:

Session.QueryOver<T>()
      .Select(Projections.Max<Statistic>(s => s.PeriodStart))
      .SingleOrDefault<object>();

หากไม่มีสิ่งใดส่งคืนจะเป็นโมฆะ ไม่เช่นนั้นให้แปลงผลลัพธ์เป็นตัวเลข

person jenson-button-event    schedule 28.09.2011