The “X” property on “Y” could not be set to a 'null' value. You must set this property to a non-null value of type 'Int32'

前端 未结 11 1347
花落未央
花落未央 2020-12-29 21:38

When I run my application and I click a specific button I get the error:

\"The \"X\" property on \"Y\" could not be set to a \'null\' value. You must set thi         


        
相关标签:
11条回答
  • 2020-12-29 22:00

    For me the following Steps corrected the Error:

    1. Remove the 'X'-Property from the 'Y'-Table
    2. Save EDMX
    3. build Database from Model
    4. compile
    5. Add the 'X'-Property to 'Y'-Table again (with non-nullable and int16)
    6. Save EDMX
    7. build Database from Model
    8. compile
    0 讨论(0)
  • 2020-12-29 22:03
     "The "X" property on "Y" could not be set to a 'null' value. You must set this property to a non-null value of type 'Int32'."
    

    In your EDMX, if you go under your Y table and click on X column, right-click, click on Properties, scroll down to Nullable and change from False to True.

    If you get a "mapping fragment" error, you'll have to delete the table from the EDMX and re-add it, because in the Model Browser it stores the table properties and the only way to refresh that (that I know of) is to delete the table from the Model Browser under <database>.Store then retrieving it using Update Model from Database.. command.

    0 讨论(0)
  • 2020-12-29 22:03

    This may happen when the database table allows NULL and there are records that have a null value and you try to read this record with EF and the mapping class does not allow a null value.

    The solution is either change the database table so that it does not allow null or change your class to allow null.

    0 讨论(0)
  • 2020-12-29 22:04

    In my case in created view in DB column that I select that contains null value I change that value by this select statement:

    Before my change

     select 
         ..., GroupUuId , ..
    

    after my change

     select 
         ..., ISNULL(GroupUuId, 0), ... 
    

    Sorry for my bad English

    0 讨论(0)
  • 2020-12-29 22:06

    I just replace data type int to int32?

    public Int32 Field{ get; set; }
    

    to

    public Int32? Field{ get; set; }
    

    and the problem is solved

    0 讨论(0)
提交回复
热议问题