Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

People also ask

Could not be set to a null value you must set this property to a non null value of type Int32?

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 .

How do you assign a value to null?

In C#, you can assign the null value to any reference variable. The null value simply means that the variable does not refer to an object in memory. You can use it like this: Circle c = new Circle(42); Circle copy = null; // Initialized ... if (copy == null) { copy = c; // copy and c refer to the same object ... }

What does properties of null mean?

If a property's definition does not set the default value of a property, its default value is null.

Why should nulls in a relation be avoided?

They should be avoided to avoid the complexity in select & update queries and also because columns which have constraints like primary or foreign key constraints cannot contain a NULL value.


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


I just replace data type int to int32?

public Int32 Field{ get; set; }

to

public Int32? Field{ get; set; }

and the problem is solved


My problem was that my Model database was out of sync with the actual (dev) database. So the EDMX thought it was smallint but the actual column was int. I updated the model database to int and the EDMX to Int32 and now it works.


For future readers.

I got this error when I had a multiple result stored procedure.

As seen here:

http://msdn.microsoft.com/en-us/data/jj691402.aspx

If you try to access an item in the first-result, after doing a .NextResult, you may get this error.

From the article:

    var reader = cmd.ExecuteReader();

    // Read Blogs from the first result set
    var blogs = ((IObjectContextAdapter)db)
        .ObjectContext
        .Translate<Blog>(reader, "Blogs", MergeOption.AppendOnly);   


    foreach (var item in blogs)
    {
        Console.WriteLine(item.Name);
    }        

    // Move to second result set and read Posts
    reader.NextResult();
    var posts = ((IObjectContextAdapter)db)
        .ObjectContext
        .Translate<Post>(reader, "Posts", MergeOption.AppendOnly);


    foreach (var item in posts)
    {
        Console.WriteLine(item.Title);
    }

Now, if before the line

foreach (var item in posts)

you put in this code

Blog foundBlog = blogs.FirstOrDefault();

I think you can simulate the error.

Rule of Thumb:

You still gotta treat this thing like a DataReader (fire-hose).

For my needs, I had to convert to a List<>.

So I changed this:

    foreach (var item in blogs)
    {
        Console.WriteLine(item.Name);
    }  

to this:

    List<Blog> blogsList = blogs.ToList();
    foreach (var item in blogsList )
    {
        Console.WriteLine(item.Name);
    }  

And I was able to navigate the objects without getting the error.

Here is another way I encountered it.

    private void DoSomething(ObjectResult<Blog> blogs, ObjectResult<Post> posts)
    {

    }

And then after this code (in the original sample)

foreach (var item in posts)
{
    Console.WriteLine(item.Title);
}

put in this code:

DoSomething(blogs,posts);

If I called that routine and started accessing items/properties in the blogs and posts, I would encounter the issue. I understand why, I should have caught it the first time.


I verified that the entity was pointing at the correct database.

I then deleted the table from the .edmx file and added it again.

Problem solved.


Check your model & database both should be defined accordingly....

public Int32? X { get; set; } ----> Nullable Accordingly in DB 'X' should be Nullable = True

or

public Int32 X { get; set; } ----> not Nullable Accordingly in DB 'X' should be Nullable = false


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