[英]Null reference exception setting Entity Framework property
在將不可為空的標量變量分配給實體框架域對象屬性時,我們收到了不一致的空引用異常。 我了解空引用異常是什么,但我無法理解在將非空值分配給非空 EF 域對象的屬性時如何獲得異常引用。
域對象如下所示:
[DataContract]
[Table("Participant", Schema = "tour")]
public partial class Participant
{
public Participant()
{
}
[DataMember]
[Key]
public int ParticipantID { get; set; }
...
public DateTime? AutopayLastAttemptedUtc { get; set; }
}
我們遇到異常的區域是:
DateTime utcNow = DateTime.UtcNow;
// The below line throws the Null Reference Exception.
participant.AutopayLastAttemptedUtc = utcNow;
ParticipantService.Update(participant);
如果我添加一些調試代碼來證明變量不為空,我仍然會在同一行上得到一個異常:
DateTime utcNow = DateTime.UtcNow;
// This line proves that participant is not null and that
// AutopayLastUpdatedUtc can be read (not that that really
// matters because we only want to set it
TelemetryClient.TrackTrace("Participant", SeverityLevel.Information, new Dictionary<string, string> {
{ "ParticipantId", participant.ParticipantID.ToString() },
{ "AutopayLastUpdatedUtc", participant.AutopayLastAttemptedUtc.ToString() }
});
// The below line throws the Null Reference Exception.
participant.AutopayLastAttemptedUtc = utcNow;
ParticipantService.Update(participant);
我不明白什么可能為空
請注意,我不是在問什么是空引用異常。 我在問在將非空值分配給非空對象的屬性時如何可能發生空引用異常。
盡管已經檢查了幾種不同的方式,@MartinLiversage 是正確的,報告的行號已關閉。 該異常實際上是由后面的一行(未在示例代碼中顯示)拋出的,一旦我找到了有問題的行,就很容易修復。
我重現這個的測試用例是在我們的測試服務器上而不是我的開發機器上。 這是相當正常的,因為數據庫是不同的,並且在我的開發數據庫上找到觸發相同問題的數據庫記錄並不總是那么容易。 不過,它確實使調試變得更加困難。 測試服務器構建時設置了“定義調試常量”,但它也設置了“優化代碼”,所以我想知道這是否是導致錯誤報告行的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.