我在我的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