How add AllowEmptyStrings to the Required Data Annotation?

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
CodeSlinger
Posts: 31
Joined: Mon 04 May 2015 18:49

How add AllowEmptyStrings to the Required Data Annotation?

Post by CodeSlinger » Tue 02 Jun 2015 20:38

I am new to Entity Developer and am using the DbContext template, and have Validation Framework = Data Annotations set to add the Required attribute for non-nullable string fields for SQL Server via EF6.

I have run into a validation issue when I validate using mDbContext.GetValidationErrors() when trying to add new objects with empty strings after I asked Entity Developer to add the Required Message resulting in the Required Data Annotation. This is normal EF behaviour but I want to turn it off using AllowEmptyStrings.

1. What is the best way to add Required(AllowEmptyStrings=true) for all generated class properties?
2. What is the best way to add Required(AllowEmptyStrings=true) for only some classes and properties?

I was not able to search the forums for AllowEmptyStrings and Allow Empty Strings did not find anything.

Thanks, Dave

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: How add AllowEmptyStrings to the Required Data Annotation?

Post by Shalex » Wed 03 Jun 2015 13:21

Please navigate to Model > Settings > Attributes > select the C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.ComponentModel.DataAnnotations.dll assembly and make sure that the Required attribute is checked in the window below, press OK.
Then, select the Required attribute in the Attributes collection of each class property you want to apply the attribute to. The Attributes window also allows to set the attribute properties (like Allow Empty Strings).

CodeSlinger
Posts: 31
Joined: Mon 04 May 2015 18:49

Re: How add AllowEmptyStrings to the Required Data Annotation?

Post by CodeSlinger » Fri 05 Jun 2015 14:10

A few more questions please -

1. If I select System.ComponentModelDataAnnotations from the GAC (4.0.0.0) I get Attributes listed. Is that Ok or must I select the one in the Programs x86 folder? They both do what is described below.

2. Why is System.ComponentModelDataAnnotations listed twice in the "Available Attribute Types"? The first has some of the Attributes like StringLength and the second has many more including Required.

3. When I try to select the RequiredAttribute I get a dialog saying "Attribute of this type cannot be used for class in code". I see the "AllowEmtpytStrings" under properties grayed out. This happens at the top level as well as the individual class level Attributes.

4. Assuming I get this to work somehow, is the proper way to set Attributes for all of the classes at once to set Attributes at the top level and then for individual classes to set Attributes at the Class level?

Thanks, Dave

CodeSlinger
Posts: 31
Joined: Mon 04 May 2015 18:49

Re: How add AllowEmptyStrings to the Required Data Annotation?

Post by CodeSlinger » Fri 05 Jun 2015 14:30

I see where I can set Required(AllowEmptyStrings) on a given class property but now I get two Required DataAnnotations for the property I added Required(AllowEmptyStrings) on causing a compile error, one with AllowEmptyStrings set. Probably because I have DbContext Validation Error Messages set for the Required Message" and Required for the specific property getting this error.

Also is there anyway to set AllowEmptyStrings for for all string properties that get Required?

Thx.

CodeSlinger
Posts: 31
Joined: Mon 04 May 2015 18:49

Re: How add AllowEmptyStrings to the Required Data Annotation?

Post by CodeSlinger » Fri 05 Jun 2015 15:02

And ideally to also be able to add "This property is required." like the DbContext Validation Error Message does.

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: How add AllowEmptyStrings to the Required Data Annotation?

Post by Shalex » Tue 09 Jun 2015 12:29

CodeSlinger wrote:1. If I select System.ComponentModelDataAnnotations from the GAC (4.0.0.0) I get Attributes listed. Is that Ok or must I select the one in the Programs x86 folder? They both do what is described below.
That is OK. You may select any assembly with attributes you want.
CodeSlinger wrote:2. Why is System.ComponentModelDataAnnotations listed twice in the "Available Attribute Types"? The first has some of the Attributes like StringLength and the second has many more including Required.
Looks like there are two System.ComponentModelDataAnnotations.dll on your workstation. Compare versions and locations of these assemblies in your list.
CodeSlinger wrote:3. When I try to select the RequiredAttribute I get a dialog saying "Attribute of this type cannot be used for class in code". I see the "AllowEmtpytStrings" under properties grayed out. This happens at the top level as well as the individual class level Attributes.
The RequiredAttribute is designed for class properties. Apply it via the Attributes collection of a particular class property.
CodeSlinger wrote:4. Assuming I get this to work somehow, is the proper way to set Attributes for all of the classes at once to set Attributes at the top level and then for individual classes to set Attributes at the Class level?
The top level is the Validation Framework property of the template.
CodeSlinger wrote:I see where I can set Required(AllowEmptyStrings) on a given class property but now I get two Required DataAnnotations for the property I added Required(AllowEmptyStrings) on causing a compile error, one with AllowEmptyStrings set. Probably because I have DbContext Validation Error Messages set for the Required Message" and Required for the specific property getting this error.
Please set the Validation Framework property of the template to None.
CodeSlinger wrote:And ideally to also be able to add "This property is required." like the DbContext Validation Error Message does.
The RequiredAttribute includes the Error Message property. Initialize it with the "This property is required." text.

Post Reply