我在我的MVC2應用程序中使用Linq to SQL(我對這兩個都是新手)收到以下錯誤。我連接到一個實際的SQL服務器不奇怪MDF:空值不能分配給類型System.Int64這是一個非空值類型的成員
System.InvalidOperationException The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type
我的SQL表有一個稱爲郵件ID列。這是BigInt有型,有一個主鍵,NOT NULL和身份11,沒有默認
在我的DBML設計師有此字段如下聲明:
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_MessageId", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public long MessageId
{
get
{
return this._MessageId;
}
set
{
if ((this._MessageId != value))
{
this.OnMessageIdChanging(value);
this.SendPropertyChanging();
this._MessageId = value;
this.SendPropertyChanged("MessageId");
this.OnMessageIdChanged();
}
}
}
它總是告訴我,空不能分配 - 我沒有通過null!這很長 - 它甚至不可能是空的!
我在做一些愚蠢的事情嗎?我無法在任何地方找到解決方案!
我做了這個工作,通過改變這個屬性的類型爲Nullable<long>
,但肯定這是不對的?
更新: 我正在使用InsertOnSubmit。簡化代碼:
public ActionResult Create(Message message)
{
if (ModelState.IsValid)
{
var db = new MessagingDataContext();
db.Messages.InsertOnSubmit(message);
db.SubmitChanges(); //line 93 (where it breaks)
}
}
Breaking on SubmitChanges()with this error at the top of this question。
UPDATE2: 堆棧跟蹤:
at Read_Object(ObjectMaterializer`1)
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item)
at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at Qanda.Controllers.MessagingController.Ask(Message message) in C:\Qanda\Qanda\Controllers\MessagingController.cs:line 93
UPDATE3: 沒有人知道,我沒有足夠的影響力,以提供賞金!繼續我的ASP.NET blog。請幫忙!
您是否嘗試使用InsertOnSubmit方法並導致此異常? – Raja 2010-04-14 11:39:47
你能顯示導致錯誤的代碼嗎? – Geoff 2010-04-14 11:43:24
謝謝。我已經更新了我的問題 – BritishDeveloper 2010-04-14 11:53:20