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:
我在使用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
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
我的SQL表有一个名为MessageID的列。它是BigInt类型并且具有主键,NOT NULL和IDENTITY 1 1,没有默认值
In my dbml designer it has the following declaration for this field:
在我的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();
}
}
}
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!
它一直告诉我无法分配null - 我没有通过null!它很长 - 它甚至不能为空!
Am I doing something stupid? I can't find a solution anywhere!
我做了些蠢事吗?我无法在任何地方找到解决方案!
I made this work by changing the type of this property to Nullable<long>
but surely this can't be right?
我通过将此属性的类型更改为Nullable
Update: I am using InsertOnSubmit. Simplified code:
更新:我正在使用InsertOnSubmit。简化代码:
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.
使用此问题顶部的错误中断SubmitChanges()。
Update2: Stack trace:
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: No one knows and I don't have enough clout to offer a bounty! So continued on my ASP.NET blog. Please help!
Update3:没有人知道,我没有足够的影响力来提供赏金!继续在我的ASP.NET博客上。请帮忙!
4 个解决方案
#1
6
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
它花了一段时间,但我发现这种情况正在发生,并且我认为我会分享。这是因为该表在插入时有一个触发器。我在这里更详细地写了关于触发器的乐观并发异常。虽然这是实体框架,但我仍然确定这是触发器从一开始就让我感到沮丧
#2
2
The value types you have defined in your Result class need to be setup as nullable.
您在Result类中定义的值类型需要设置为可为空。
In your case use int64?
在你的情况下使用int64?
Check out the object code in your designer.cs file that was generated by the designer, if you are using it.
如果您正在使用它,请查看设计器生成的designer.cs文件中的目标代码。
Modify this code and place it in a partial class so the designer will not over write the code.
修改此代码并将其放在部分类中,以便设计人员不会过度编写代码。
#3
0
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
我有这个问题调用一个存储过程返回3个表的数据与左外连接问题是一个Bigint类型的列是空的所以我在过程中应用ISNULL(OID,0)克服在LINQ
#4
0
Check first which column is cause of exception then set its default value in table or EF Model if you are using it.
首先检查哪个列是异常原因然后在表或EF模型中设置其默认值(如果您正在使用它)。
If you save nothing for this column it will store with default value and this error will never occur again.
如果您为此列保存任何内容,它将以默认值存储,并且此错误将永远不会再次发生。
#1
6
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
它花了一段时间,但我发现这种情况正在发生,并且我认为我会分享。这是因为该表在插入时有一个触发器。我在这里更详细地写了关于触发器的乐观并发异常。虽然这是实体框架,但我仍然确定这是触发器从一开始就让我感到沮丧
#2
2
The value types you have defined in your Result class need to be setup as nullable.
您在Result类中定义的值类型需要设置为可为空。
In your case use int64?
在你的情况下使用int64?
Check out the object code in your designer.cs file that was generated by the designer, if you are using it.
如果您正在使用它,请查看设计器生成的designer.cs文件中的目标代码。
Modify this code and place it in a partial class so the designer will not over write the code.
修改此代码并将其放在部分类中,以便设计人员不会过度编写代码。
#3
0
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
我有这个问题调用一个存储过程返回3个表的数据与左外连接问题是一个Bigint类型的列是空的所以我在过程中应用ISNULL(OID,0)克服在LINQ
#4
0
Check first which column is cause of exception then set its default value in table or EF Model if you are using it.
首先检查哪个列是异常原因然后在表或EF模型中设置其默认值(如果您正在使用它)。
If you save nothing for this column it will store with default value and this error will never occur again.
如果您为此列保存任何内容,它将以默认值存储,并且此错误将永远不会再次发生。