none
Validation not working when removing a nullable required field EF 6.1.1 RRS feed

  • Question

  • EF 6.1.1, VS2013 Ultimate

    Here is my model, the reason I have a nullable required field called RoleID is because it works best when adding a new records, the RoleID is a FK therefore on an add screen the default value is nothing meaning nothing is selected rather than showing 0 and having to add logic to handle a 0 value. Then its required because the user must make a selection and its NOT NULL in the DB.

    <Key>
    Public Property UserID As Integer

    <Required>
    Public Property RoleID As Integer?

    The Problem

    'Code works without issue. delete.RoleID = 1
    Dim delete As Users = MyBase.Users.SingleOrDefault(Function(Users) Users.UserID = Item.UserID)

    'When this line is executed the 'delete' object is returned with a RoleID of nothing when it should be delete.RoleID = 1
    Users.Remove(delete)

    'SaveChanges fires the Validation method and of course RoleID is required but has no value therefore it throws the validation exception
    SaveChanges()

    Everything of course works without issue when there is no nullable or the field is not required.

    Anyone know how to get around this validation error when using a nullable int required field? Looking for an elegant solution that does not consist of adding logic to the validation method.

    Thursday, July 10, 2014 4:32 PM

Answers

  • Hello,

    >>This is probably a grey area in EF, not sure. Really need to be able to leverage NULLABLE REQUIRED fields when working within the code. Use NULLABLE when assembling screens and REQUIRED when the database transactions actually occurs.

    I am afraid current version does not support such a scenario, you could post a wish to:

    http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions

    And involve others to vote it so that its priority would be upgraded.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 14, 2014 7:37 AM
    Moderator

All replies

  • Hello,

    From your description, I feel confused because you said the RoleID is NOT NULL in DB while the generated property is nullable.

    Whether you are using database first or code first, if you share your current table schemas and entities model, we could understand your question better and help you more effective.

    >>'SaveChanges fires the Validation method and of course RoleID is required but has no value therefore it throws the validation exception

    What is the detail error message? Please share it with us.

    >> Anyone know how to get around this validation error when using a nullable int required field? Looking for an elegant solution that does not consist of adding logic to the validation method.

    You may have a try to disable the validation as set ValidateOnSaveEnabled to false:

    DbContext.Configuration.ValidateOnSaveEnabled = False

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 11, 2014 2:20 AM
    Moderator
  • From your description, I feel confused because you said the RoleID is NOT NULL in DB while the generated property is nullable.

    I am using CodeFirst. So yes, I am breaking the model (maybe) as I do not do migrations from the Code. As a nullable field that is required is definitely not intuitive and EF looks to be confused by this.

    [RoleID] [int] NOT NULL

    <Required>
     Public Property RoleID As Integer?

    -----------

    >>'SaveChanges fires the Validation method and of course RoleID is required but has no value therefore it throws the validation exception

    What is the detail error message? Please share it with us.

    The message is "RoleID is a required field", just standard DataAnnotation message for a required field.

    -------------

    You may have a try to disable the validation as set ValidateOnSaveEnabled to false:

    This would solve this issue however I need the validation turned on even for deletion for other rules.

    ---------------

    This is probably a grey area in EF, not sure. Really need to be able to leverage NULLABLE REQUIRED fields when working within the code. Use NULLABLE when assembling screens and REQUIRED when the database transactions actually occurs.

    Friday, July 11, 2014 2:13 PM
  • Hello,

    >>This is probably a grey area in EF, not sure. Really need to be able to leverage NULLABLE REQUIRED fields when working within the code. Use NULLABLE when assembling screens and REQUIRED when the database transactions actually occurs.

    I am afraid current version does not support such a scenario, you could post a wish to:

    http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions

    And involve others to vote it so that its priority would be upgraded.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 14, 2014 7:37 AM
    Moderator
  • What I am trying to do is not supported in EF.

    Submitted feature request to EF team. Vote for it if you want the feature.

    https://entityframework.codeplex.com/workitem/2398


    Monday, July 14, 2014 12:25 PM