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
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.
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.
Got same error but different context, tried to join tables using linq where for one of the tables in database, a non-null column had a null value inserted, updated the value to default and the issue is fixed.
to fix the error
Error 3031: Problem in mapping fragments starting at line 4049:Non-nullable column "X" in table "Y" is mapped to a nullable entity property.
open your EDMX file with and xml editor and lookup you table in
edmx:StorageModels
find the propertie which gives the error and set or add
Nullable="false" >> to Nullable="true"
save the edmx, open it in visual studio and build it. 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
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.