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'

entity-framework sql visual-studio-2010

Question

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 this property to a non-null value of type 'Int32'."

Cool so I go to my Entity project, go to Y table, find X column, right-click and go to X's properties and find that Nullable is set to False.

I verify in SQL that in Y table, X is set to allow nulls, and it is.

I then go back to my Entity project, set Nullable to True, save and build and I receive:

Error 3031: Problem in mapping fragments starting at line 4049:Non-nullable column "X" in table "Y" is mapped to a nullable entity property.

I've heard that deleting the table from the .edmx file and then re-adding it is a possibility but have never done that and don't understand the implications enough to feel comfortable in doing that.

I've heard that it could be in the view, could be in the stored procedure...

Also have heard that this is a bug.

Has anyone come across this and found an "across the board" fix or somewhat of a road map of sorts on where to look for this error?

Thanks!

1
27
11/16/2012 3:56:41 PM

Accepted Answer

 "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.

29
11/16/2012 6:23:08 PM

Popular Answer

I just replace data type int to int32?

public Int32 Field{ get; set; }

to

public Int32? Field{ get; set; }

and the problem is solved



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow