locked
Correct usage of required attribute with EF and WCF RIA Services RRS feed

  • Question

  • I posted this thread already on the entity framework forums but the answers I got back didn't make much sense to me especially since I am using RIA services:

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/1badeede-8813-45f4-b30c-582f1fb62b52

    So thought I would ask the question again here:

    So as the title says really how do you use the [Requried] attribute when you have a field which is NOT NULL in the database (I'm using EF database first (.edmx)).

    Since the field in the database is marked as NOT NULL, EF will create the model with the foreign key Id a non nullable so that it matches the database which makes sense.  However if I then in my metadata class try to attribute the foreigh key with required I get the error

    "Association X is marked with the RequiredAttribute.  Please remove the atribute and mark the underlying foreign key memebers with RequiredAttribute instead."

    So if I do the above I then mark the foreign key id field with [Required] instead but because this is not nullable on the client the default value will always be 0 and as such the validation will never trigger.

    Some code examples:

    [MetadataType(typeof(Allergy.AllergyMetadata))]
        public partial class Allergy
        {
            /// <summary>
            /// The allergy metadata
            /// </summary>
            internal sealed class AllergyMetadata
            {
                /// <summary>
                /// Gets or sets the id.
                /// </summary>
                /// <value>
                /// The id.
                /// </value>
                [Key]
                public long Id { getset; }
                /// <summary>
                /// Gets or sets the allergy severity.
                /// </summary>
                /// <value>
                /// The allergy severity.
                /// </value>
                [Include]
                [Association("AllergyToAllergySeverity""SeverityId""Id", IsForeignKey = true)]
                public AllergySeverity AllergySeverity { getset; }

    I was hoping I would be able to set SeverityId as nullable in the model only, but when I do this I get loads of errors in the edmx file so that makes me think this isn't the correct approach.  I presume I would just change the property Nullable = True for SeverityId and also the muliplicity of the association to 0..1.  But again this resulted in errors.  E.g.

    Error    52    Error 3031: Problem in mapping fragments starting at line 3636:Non-nullable column Allergy.SeverityId in table Allergy is mapped to a nullable entity property

    Wednesday, July 11, 2012 5:46 AM

Answers

  • So as the title says really how do you use the [Requried] attribute when you have a field which is NOT NULL in the database (I'm using EF database first (.edmx)).

    RIA Services will extract the Required attribute from the EDMX on its own, you generally don't need to add it yourself. For value types (integer, etc) the Required is not needed because value types are always required by definition. Generally, the only fields that need a Required on them are string values.

    "Association X is marked with the RequiredAttribute.  Please remove the atribute and mark the underlying foreign key memebers with RequiredAttribute instead."

    The reason for that is that entities can be loaded on their own. So, if entity A has an assocition with entity B you can load just Entity A from the server. Because the foreign key has a value the association is still good, but the Entity B property will be null since that entity isn't actually loaded.

    So if I do the above I then mark the foreign key id field with [Required] instead but because this is not nullable on the client the default value will always be 0 and as such the validation will never trigger.

    That is correct, and technically speaking there isn't anything you can do about it. As I said above, a Required attribute doesn't do you any good for a value type. You could try using a RangeAttribute instead or add you own custom validator. I would recommend inheriting from the RequiredAttribute and changing the logic to not allow a 0.

    Monday, July 16, 2012 2:02 PM

All replies

  • So as the title says really how do you use the [Requried] attribute when you have a field which is NOT NULL in the database (I'm using EF database first (.edmx)).

    RIA Services will extract the Required attribute from the EDMX on its own, you generally don't need to add it yourself. For value types (integer, etc) the Required is not needed because value types are always required by definition. Generally, the only fields that need a Required on them are string values.

    "Association X is marked with the RequiredAttribute.  Please remove the atribute and mark the underlying foreign key memebers with RequiredAttribute instead."

    The reason for that is that entities can be loaded on their own. So, if entity A has an assocition with entity B you can load just Entity A from the server. Because the foreign key has a value the association is still good, but the Entity B property will be null since that entity isn't actually loaded.

    So if I do the above I then mark the foreign key id field with [Required] instead but because this is not nullable on the client the default value will always be 0 and as such the validation will never trigger.

    That is correct, and technically speaking there isn't anything you can do about it. As I said above, a Required attribute doesn't do you any good for a value type. You could try using a RangeAttribute instead or add you own custom validator. I would recommend inheriting from the RequiredAttribute and changing the logic to not allow a 0.

    Monday, July 16, 2012 2:02 PM
  • I am marking this thread as answered. If you need further assistance, please visit the below link to see the various support options that are available to meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Thursday, August 2, 2012 1:15 PM