开发者

The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type

I'm getting the following error in my MVC2 app using Linq to SQL (I am new to both). I am connected to an actual SQL server not weird mdf:

System.InvalidOperationException The null value cannot be assigned to a member with type System.Int64 which is a non-nullable value type

My SQL table has a column called MessageID. It is BigInt type and has a primary key, NOT NULL and an IDENTITY 1 1, no Default

In my dbml designer it has the following declaration for this field:

[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();
        }
    }
}

It keeps telling me that null cannot be assigned - I'm not passing through null! It's a long - it can't even be null!

Am I doing something stupid? I can't find a solution anywhere!

I made this work 开发者_JS百科by changing the type of this property to Nullable<long> but surely this can't be right?

Update: I am using InsertOnSubmit. Simplified code:

public ActionResult Create(Message message)
{
    if (ModelState.IsValid)
    {
       var db = new MessagingDataContext();
       db.Messages.InsertOnSubmit(message);
       db.SubmitChanges(); //line 93 (where it breaks)
    }
}

breaks on SubmitChanges() with the error at the top of this question.

Update2: Stack trace:

   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: No one knows and I don't have enough clout to offer a bounty! So continued on my ASP.NET blog. Please help!


It has taken a while but I have discovered this was happening and thought I'd share. It was because the table had a trigger on insert. I've wrote about it in more detail here optimistic concurrency exception with triggers. Although this is with entity framework, I'm still sure it is the trigger causing my dismay from the start


The value types you have defined in your Result class need to be setup as nullable.

In your case use int64?

Check out the object code in your designer.cs file that was generated by the designer, if you are using it.

Modify this code and place it in a partial class so the designer will not over write the code.


I got this problem calling a stored procedure returning data of 3 tables with Left outer join the problem was that a Column with Bigint Type was Null So I applied ISNULL(OID,0) in the procedure to overcome in LINQ


Check first which column is cause of exception then set its default value in table or EF Model if you are using it.

If you save nothing for this column it will store with default value and this error will never occur again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜