阻止实体框架 4 中的验证

发布于 2024-10-06 20:51:19 字数 993 浏览 0 评论 0原文

我正在使用实体框架 4 和动态数据站点向一些用户公开一个简单的管理界面。总体来说工作得很好,但是我在模型的几个字段上遇到了这个问题。

多个表具有一些与审核相关的字段 - CreatedBy、CreatedDate、ModifiedBy 和 ModifiedDate。这些字段在数据库中是必需的,并且关联的模型将属性标记为不可为空(一切都应该如此)。但是,我正在代码中设置这些字段的值 - 字段类型的字段模板将这些特定字段标记为在页面上禁用,并且在 SavingChanges 事件中我将这些字段设置为适当的值。当我更新现有项目时,一切都很好。

当我尝试创建新项目时,问题就出现了。我希望这些字段在页面上保持为空,并在提交时由我的代码自动填充,但字段模板为这些字段设置了RequiredFieldValidators,并且不允许我在没有值的情况下提交它们。通常这会很棒,但我想阻止 EF 在页面提交时验证这些字段。

我意识到我可以在数据库中将字段标记为可为空,这将解决问题 - 从数据的角度来看,它甚至可能很好,但我对此感到不舒服 - 一方面,某些情况并非不可能这些字段出现的模型将在稍后批量加载,可能由其他人加载。我宁愿让数据库强制这些字段的不可空性。在字段模板中,我尝试移动内置 SetUpValidator() 调用,以便在加载这些特定字段时不要运行RequiredFieldValidator,并且我还尝试禁用RequiredFieldValidators 并强制其IsValid 属性为true。这些操作都不允许我提交页面。

有没有办法告诉 EF/动态数据跳过某些字段的验证?

编辑

如下所述,我还尝试在模型中将它们标记为可为空,而不是在数据库中,这导致了错误:映射片段中的问题...不可为空列...中表...映射到可为空的实体属性。

编辑 #2

我找到了一个可行的解决方案,但需要修改实体集自动生成的设计器文件,即充其量是脆弱的。我很想知道一种“正确”的方法来做到这一点,但如果在接下来的几天内没有任何明显的结果,我将发布我自己的答案。

I'm using Entity Framework 4 and a Dynamic Data site to expose a bare-bones admin interface to a few users. Working pretty well in general, but I have run into this one problem on a couple of fields on my model.

Several tables have some audit-related fields - CreatedBy, CreatedDate, ModifiedBy, and ModifiedDate. These fields are required in the database and the associated models are marking the properties as non-nullable (all as it should be). However I am handing setting the values for these fields in code - the field templates for the field types mark these specific fields as disabled on the page, and in the SavingChanges event I set these fields to the appropriate values. All works great when I'm updating an existing item.

The problem comes in when I try to create a new item. I want these fields to remain empty on the page and be auto-populated by my code when submitted, but the Field Templates set up RequiredFieldValidators for these fields and won't let me submit them without a value. Normally this would be great, except that I want to prevent EF from validating these fields at the point of page submission.

I realize that I could mark the fields as nullable in the database and that would resolve the issue - it would probably even be just fine from the data standpoint, but I'm not comfortable with doing so - for one thing it's not unlikely that some of the models these fields appear on will be bulk loaded, possibly by someone else, at a later date. I would rather still have the database enforce the non-nullability of these fields. In the field templates I've tried moving the built-in SetUpValidator() call for the RequiredFieldValidator not to run when these specific fields are being loaded, and I've also tried disabling the RequiredFieldValidators and forcing their IsValid property to true. None of these actions allows me to submit the page.

Is there a way to tell EF/Dynamic Data to skip the validation for some fields?

EDIT

As noted below, I also tried marking them nullable in the model and not in the database, which caused an error: Problem in mapping fragments...Non-nullable column...in table...is mapped to a nullable entity property.

EDIT #2

I have found a solution that works, but requires modifying the auto-generated designer file for the entity set, which is fragile at best. I would love to know a "righter" way to do it, but if nothing becomes apparent in the next couple of days I'll post my own answer.

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

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

发布评论

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

评论(2

淡墨 2024-10-13 20:51:19

以下是我发现必须进行的编辑。当允许该工具在 edmx Designer.cs 文件中创建实体时,我得到如下属性:

对于服务器端的日期时间

 [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
 [DataMemberAttribute()]
 public global::System.DateTime CreatedDate
 {
    get
    {
       return _CreatedDate;
    }
    set
    {
       OnCreatedDateChanging(value);
       ReportPropertyChanging("CreatedDate");
       _CreatedDate = StructuralObject.SetValidValue(value);
       ReportPropertyChanged("CreatedDate");
       OnCreatedDateChanged();
    }
 }

对于 varchar

 [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
 [DataMemberAttribute()]
 public global::System.String CreatedBy
 {
    get
    {
       return _CreatedBy;
    }
    set
    {
       OnCreatedByChanging(value);
       ReportPropertyChanging("CreatedBy");
       _CreatedBy = StructuralObject.SetValidValue(value, false);
       ReportPropertyChanged("CreatedBy");
       OnCreatedByChanged();
    }
 }

要使其在不验证 DateTime 属性的情况下工作,将 EdmScalarPropertyAttribute 的 IsNullable 参数设置为 true足以避免该问题。对于 String 属性,您还必须更改 SetValidValue 的第二个参数方法调用为“true”。

综上所述,我保持原样的唯一原因是因为我不希望在我们移动到该站点的不同平台之前重新生成实体超过一次或两次。在这种情况下,将我已签入 git 的版本与该工具生成的版本合并可以让我避免大部分令人头疼的问题,

So here are the edits I found I had to make. When allowing the tool to create the entities in the edmx Designer.cs file I get properties like these:

for a datetime on the server side

 [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
 [DataMemberAttribute()]
 public global::System.DateTime CreatedDate
 {
    get
    {
       return _CreatedDate;
    }
    set
    {
       OnCreatedDateChanging(value);
       ReportPropertyChanging("CreatedDate");
       _CreatedDate = StructuralObject.SetValidValue(value);
       ReportPropertyChanged("CreatedDate");
       OnCreatedDateChanged();
    }
 }

for a varchar

 [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
 [DataMemberAttribute()]
 public global::System.String CreatedBy
 {
    get
    {
       return _CreatedBy;
    }
    set
    {
       OnCreatedByChanging(value);
       ReportPropertyChanging("CreatedBy");
       _CreatedBy = StructuralObject.SetValidValue(value, false);
       ReportPropertyChanged("CreatedBy");
       OnCreatedByChanged();
    }
 }

To make it work without validation for a DateTime property setting the IsNullable parameter of the EdmScalarPropertyAttribute to true is sufficient to avoid the issue. For the String property you also have to change the 2nd parameter of the SetValidValue method call to "true."

All of this said, the only reason that I'm leaving this as it is is because I don't expect to have to regenerated the entities more than once or twice before we move to a different platform for this site. And in this case, merging the version in I have checked in to git with the version generated by the tool allows me to avoid most of the headaches,

萌面超妹 2024-10-13 20:51:19

这是我的只读自动生成日期字段的元信息。我没有得到验证这些字段的验证控件。希望这有帮助。

[ReadOnly(true)]
[DataType(DataType.Date)]
[Column(IsDbGenerated = true, UpdateCheck = UpdateCheck.Never, AutoSync = AutoSync.Never)]
[UIHint("DateTime")]
[Display(Name = "Modified", Order = 1000)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public object DateModified { get; private set; }

Here is my meta information for a read-only auto generated date field. I don't get validation controls validating these fields. Hope this helps.

[ReadOnly(true)]
[DataType(DataType.Date)]
[Column(IsDbGenerated = true, UpdateCheck = UpdateCheck.Never, AutoSync = AutoSync.Never)]
[UIHint("DateTime")]
[Display(Name = "Modified", Order = 1000)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public object DateModified { get; private set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文