SQL “Y”上的“X”属性无法设置为“空”值。您必须将此属性设置为“Int32”类型的非空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13420037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:09:12  来源:igfitidea点击:

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'

sqlvisual-studio-2010entity-framework

提问by Mark Saluta

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.

很酷,所以我转到我的实体项目,转到 Y 表,找到 X 列,右键单击并转到 X 的属性,发现 Nullable 设置为 False。

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

我在 SQL 中验证,在 Y 表中,X 设置为允许空值,并且确实如此。

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

然后我回到我的实体项目,将 Nullable 设置为 True,保存并构建,然后我收到:

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.

我听说从 .edmx 文件中删除表格然后重新添加它是一种可能性,但从未这样做过,并且不明白其中的含义,无法轻松地做到这一点。

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!

谢谢!

回答by Bob.

 "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 Nullableand change from Falseto True.

在您的 EDMX 中,如果您在 Y 表下单击 X 列,右键单击,单击属性,向下滚动到Nullable并从 更改FalseTrue

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>.Storethen retrieving it using Update Model from Database..command.

如果出现“映射片段”错误,则必须从 EDMX 中删除表并重新添加它,因为在模型浏览器中它存储表属性以及刷新它的唯一方法(我知道)是从模型浏览器中删除表,<database>.Store然后使用Update Model from Database..命令检索它。

回答by Yasir Antaal

I just replace data type intto int32?

我只是将数据类型替换intint32?

public Int32 Field{ get; set; }

to

public Int32? Field{ get; set; }

and the problem is solved

问题解决了

回答by PeterX

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

我的问题是我的模型数据库与实际(开发)数据库不同步。所以 EDMX 认为它是smallint但实际的列是 int。我将模型数据库更新为intEDMX Int32,现在它可以工作了。

回答by granadaCoder

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

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.

如果您尝试访问 first-result 中的项目,在执行 a 后.NextResult,您可能会收到此错误。

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 beforethe 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).

你还是要把这东西当作DataReader(消防软管)来对待。

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

根据我的需要,我不得不转换为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.

如果我调用该例程并开始访问博客和帖子中的项目/属性,我会遇到该问题。我明白为什么,我应该第一次抓住它。

回答by Mark Saluta

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

我确认该实体指向正确的数据库。

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

然后我从 .edmx 文件中删除了该表并再次添加了它。

Problem solved.

问题解决了。

回答by Ali Sadri

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

在我的情况下,在我选择的包含空值的 DB 列中创建的视图中,我通过以下选择语句更改了该值:

Before my change

在我改变之前

 select 
     ..., GroupUuId , ..

after my change

在我改变之后

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

Sorry for my bad English

对不起,我的英语不好

回答by redM76

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
  1. 从“Y”表中删除“X”属性
  2. 保存 EDMX
  3. 从模型构建数据库
  4. 编译
  5. 再次将“X”-属性添加到“Y”-表(具有不可为空和 int16)
  6. 保存 EDMX
  7. 从模型构建数据库
  8. 编译

回答by baalu

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.

遇到相同的错误但上下文不同,尝试使用 linq 连接表,其中对于数据库中的一个表,非空列插入了空值,将值更新为默认值,问题得到解决。

回答by Martin Staufcik

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.

当数据库表允许 NULL 并且存在具有空值的记录并且您尝试使用 EF 读取此记录并且映射类不允许空值时,可能会发生这种情况。

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

解决方案是更改数据库表,使其不允许为空,或者将您的类更改为允许为空。

回答by Izztraab

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

检查您的模型和数据库都应相应地定义....

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

公共 Int32?X{得到; 放; } ----> Nullable 因此在 DB 'X' 中应该是 Nullable = True

or

或者

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

公共 Int32 X { 得到;放; } ----> not Nullable 因此在 DB 'X' 中应该是 Nullable = false