无法将null值分配给类型为System.Int64的成员,该成员是非可空值类型

Bri*_*per 5 c# asp.net-mvc linq-to-sql

我使用Linq to SQL在我的MVC2应用程序中收到以下错误(我是两者都是新手).我连接到一个不奇怪的mdf的实际SQL服务器:

System.InvalidOperationException The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type
Run Code Online (Sandbox Code Playgroud)

我的SQL表有一个名为MessageID的列.它是BigInt类型并且具有主键,NOT NULL和IDENTITY 1 1,没有默认值

在我的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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它一直告诉我无法分配null - 我没有通过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)
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此问题顶部的错误中断SubmitChanges().

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
Run Code Online (Sandbox Code Playgroud)

Update3: 没有人知道,我没有足够的影响力来提供赏金!继续在我的ASP.NET博客上.请帮忙!

Bri*_*per 6

它花了一段时间,但我发现这种情况正在发生,并认为我会分享.这是因为该表在插入时有一个触发器.我在这里更详细地写了关于 触发器的乐观并发异常.虽然这是实体框架,但我仍然确定这是触发器从一开始就让我感到沮丧