MVC2 Entity Framework 4 中所需字符串属性的服务器端验证不起作用

发布于 2024-09-07 05:08:12 字数 1189 浏览 6 评论 0原文

我正在尝试使实体框架字符串属性的服务器端验证正常工作。其他服务器端验证(例如数据类型验证以及所需的 dateTime 和数字 EF 属性)正在运行。

这在 VS 2010、.Net 4.0、MVC2 + Cloud、ADO.Net 实体框架中。

我遇到问题的字符串属性映射到 SQL 2008 Varchar(50) 不可为空列。

当我尝试将此属性的空字符串发布到“创建”操作时,出现以下错误。

异常详细信息:System.Data.ConstraintException:此属性不能设置为空值。

当我用空格发布到操作时,我成功收到必填字段验证消息。

我尝试过使用数据注释和 ClientSideValidation,但 ClientSideValidation 在部分视图和 jquery 对话框上工作似乎存在问题。

这是实体框架的原始自动生成代码。

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String GradeTypeName
{
    get
    {
        return GradeTypeName;
    }
    set
    {
        OnGradeTypeNameChanging(value);
        ReportPropertyChanging("GradeTypeName");
        _GradeTypeName = StructuralObject.SetValidValue(value, false);
        ReportPropertyChanged("GradeTypeName");
        OnGradeTypeNameChanged();
    }
}

根据 Action 方法(CREATE 或 EDIT)的签名,异常可能发生在进入方法之前或调用 UpdateModel() 时的方法内。内部异常位于 model.designer.cs 文件的下面一行。

_GradeTypeName = StructuralObject.SetValidValue(value, false);

我已经能够在一个简单的 mvc2 Web 应用程序上重现这一点。

I'm trying to get server-side validation of an Entity Framework String Property to work. Other server-side validation such as data type validation and required dateTime and numeric EF properties are working.

This in VS 2010, .Net 4.0, MVC2 + Cloud, ADO.Net Entity Framework.

The String Property I am having issues with is mapped to a SQL 2008, Varchar(50) non-nullable column.

When I try to post to my Create action with an empty string for this Property, I get the follwing error.

Exception Details: System.Data.ConstraintException: This property cannot be set to a null value.

When I post to the action with a blank space, I successfully get a required field validation message.

I have tried using Data Annotations and ClientSideValidation but there seems to be issues with ClientSideValidation working on partial views and jquery dialogs.

Here is the orginal autogenerated code from the entity framework.

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String GradeTypeName
{
    get
    {
        return GradeTypeName;
    }
    set
    {
        OnGradeTypeNameChanging(value);
        ReportPropertyChanging("GradeTypeName");
        _GradeTypeName = StructuralObject.SetValidValue(value, false);
        ReportPropertyChanged("GradeTypeName");
        OnGradeTypeNameChanged();
    }
}

Depending on the signature of the Action method (CREATE or EDIT), the exception can occur before stepping into the method or within the method when UpdateModel() is called. The inner exception is at the line below from the model.designer.cs file.

_GradeTypeName = StructuralObject.SetValidValue(value, false);

I have been able to reproduce this on a simple mvc2 web application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

李不 2024-09-14 05:08:12

我有一段时间也遇到同样的问题。我在这里找到了一段解释: http://mvcmusicstore.codeplex.com/workitem/6604 .简而言之,Entity 的 Property Validation 会抛出异常“System.Data.ConstraintException: This property can be set to a null value”。当您的 mvc 应用程序尝试将表单字段绑定到相应的实体属性时,就会执行此验证(称为预绑定验证,在提交表单时发生)。由于字段为空(因此转换为 null),绑定器尝试将 null 值绑定到属性,这违反了实体属性的非 Null 约束。

但是,如果您使用空白字段(与空不同,因此为空)发布实体验证通过(因为该属性不再设置为空值),然后您会看到来自“必需”注释验证的消息,即在预绑定之后执行(它是绑定后验证)。

解决方法是使用注释 [DisplayFormat(ConvertEmptyStringToNull = false)] 告诉绑定器不要将空字符串转换为 null。

  [Required]
  [DisplayFormat(ConvertEmptyStringToNull = false)]
  public string YourStringProperty { get; set;}

希望这有帮助!

i was having the same problem for a while. I have found a piece of explanation here: http://mvcmusicstore.codeplex.com/workitem/6604 . To put it in a nutshell, the exception "System.Data.ConstraintException: This property cannot be set to a null value" is thrown by Entity's Property Validation. This validation is performed when your mvc application tries to bind the form field to the corresponding entity property( it's called PreBinding Validation, and it occurs when submitting the form). As the field is empty( therefore convert to null), the binder tries to bind a null value to the property, which violates the Non-Null constraint on your entity's property.

But if you post with a blank field ( that is different from empty, therefore null) Entity validation passes( as the property is not set to a null value anymore), and then your see the message from the "Required" annotation validation, that is performed after the prebinding (it's PostBinding Validation).

A workaround is to use the annotation [DisplayFormat(ConvertEmptyStringToNull = false)] that tells to the binder not to convert an empty string to null.

  [Required]
  [DisplayFormat(ConvertEmptyStringToNull = false)]
  public string YourStringProperty { get; set;}

Hope, this helps!

故事与诗 2024-09-14 05:08:12

这非常有帮助。我正在使用 MVC3 和实体框架。我将实体直接传递到控制器中,但当表单为空白时出现相同的错误。使用实体框架,您可以通过编辑自动生成的代码来进行数据验证,但创建实体的单独部分类对我来说效果更好。

[MetadataType(typeof(TestEntityValidation))]
public partial class TestEntity{
}

public class TestEntityValidation{
    [Required]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public String name { get;set}
}

This was very helpful. I'm using MVC3 and entity framework. I was passing my entities directly into the controller, but got the same error when the form was blank. With entity framework you can do data validation by editing the auto-generated code, but creating a separate partial class of the entity worked better for me.

[MetadataType(typeof(TestEntityValidation))]
public partial class TestEntity{
}

public class TestEntityValidation{
    [Required]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public String name { get;set}
}
向地狱狂奔 2024-09-14 05:08:12

有时,在 EF 中的数据库优先方法中,您可以使用 SQL 查询将列从 not null 更新为 can be null 并使用“从数据库更新模型...”(在EDMX 右键单击​​)然后该实体的属性可能未正确更新,因此如果该列中有一些 null 数据,在映射中,就会发生违规并显示此错误。

解决这个问题;您可以在更新实体的属性的属性中检查Nullable

Sometimes in database first approach in EF, may you update your column from not null to can be null using SQL query and use 'Update Model From Database...' (in EDMX right click) then maybe property of that entity not updated properly and so if you have some null data in that column ,in mapping ,violation occurs and this error shown.

To fix this; You can check the Nullable in Properties of that property of entity that you updated it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文