开发者

Preventing Validation in Entity Framework 4

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?

开发者_如何学GoEDIT

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.


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,


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; }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜