问题描述
VS 2010 Beta 2中,.NET 4。
VS 2010 Beta 2, .NET 4.
在我的ASP.NET MVC 2应用程序,当我提交表单,以接受由实体框架创建的对象的操作方法,我得到以下错误:
In my ASP.NET MVC 2 application, when I submit a form to an action method that accepts an object created by the entity framework, I get the following error:
Exception Details: System.Data.ConstraintException: This property cannot be set to a
null value.
Source Error:
Line 4500: OnTextChanging(value);
Line 4501: ReportPropertyChanging("Text");
Line 4502: _Text = StructuralObject.SetValidValue(value, false);
Line 4503: ReportPropertyChanged("Text");
Line 4504: OnTextChanged();
该物业在MS SQL 2008被称为文字,是类型文本NOT NULL。
The property is called "Text" and is of type "text NOT NULL" in MS SQL 2008.
我的行动将检查值nullorempty,如果是这样,一个模型误差将增加,但我尽快得到的错误,因为我提交表单。
My action will check if the value is nullorempty, if it is, a model error will be added, but I get the error as soon as I submit the form.
推荐答案
您直接绑定到实体?当然看起来。所以,你有两个选择:
Are you binding directly to the entity? Sure looks like it. So you have two choices:
- 在编写自定义模型粘合剂,转化为空 - >空字符串
- 绑定到编辑模式,允许空值来代替,然后当你在行动中的值复制到实体更改为空字符串。
我会选择#2,个人。我想你应该总是使用查看/编辑模式,这就是为什么一个很好的例子。
I'd choose #2, personally. I think you should always use view/edit models, and this is a great example of why.
这篇关于当提交实体框架错误空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!