我的SQL表被设置为允许此列的NULL,但是当我运行它时,它表示它不能为NULL。什么/为什么/如何? [英] My SQL table is set to allow NULL for this column, but when I run it, it says it cannot be NULL. What/Why/How?

查看:415
本文介绍了我的SQL表被设置为允许此列的NULL,但是当我运行它时,它表示它不能为NULL。什么/为什么/如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这里很奇怪的困境。我的SQL表设置为允许我的ZipCode列的空值,如下所示:

  CREATE TABLE [dbo]。[Companies] 

[CompanyId] BIGINT IDENTITY(1,1)NOT NULL PRIMARY KEY,
[名称] NVARCHAR(100)NOT NULL,
[地址] NVARCHAR(100)
[City] NVARCHAR(50)NOT NULL,
[状态] NVARCHAR(2)NOT NULL,
[ZipCode] INT NULL,
[PhoneNum] BIGINT NULL,
[CreatedDate] DATETIME2 NOT NULL DEFAULT GetDate()

这应该让我ZipCode的值为NULL,对吧?好吧,显然不是....



我不断得到这个错误:



EntityFramework.dll中出现类型System.Data.ConstraintException的异常,但未在用户代码中处理其他
信息:'Company'上的'ZipCode'属性无法设置为
'null'值。您必须将此属性设置为非空值类型
'System.Int32'。


任何人都可以想到为什么会这样做的任何原因?我已经检查并重新检查了我的数据库项目和我的本地数据库,并且它们都匹配。我所有的单位测试都通过了,所以我在这里亏损了很多。



任何帮助将不胜感激!

解决方案

您的财产在C#实体框架显然是:

  public int ZipCode; 

您必须将其更改为

  public Nullable< int>邮政编码; 

您还可以在实体框架的edmx文件的可视化编辑器的属性窗口中执行此操作。 / p>

更新:



这也是可取的(如果可能的话)将ZipCode的类型更改为字符串。它不仅会照顾当前的可空问题,而且还会随着需求的进一步下滑而改变。


so I have quite the odd predicament here. My SQL table is set to allow nulls for my ZipCode column, like so:

CREATE TABLE [dbo].[Companies]
(
    [CompanyId] BIGINT IDENTITY(1,1) NOT NULL PRIMARY KEY, 
    [Name] NVARCHAR(100) NOT NULL, 
    [Address]     NVARCHAR (100) NULL,
    [City]        NVARCHAR (50)  NOT NULL,
    [State]       NVARCHAR (2)   NOT NULL,
    [ZipCode]     INT   NULL,
    [PhoneNum]    BIGINT  NULL,
    [CreatedDate] DATETIME2 NOT NULL DEFAULT GetDate()
)

This should let me have ZipCode's value as NULL, right? Well, apparently not....

I keep getting this error:

An exception of type 'System.Data.ConstraintException' occurred in EntityFramework.dll but was not handled in user code Additional information: The 'ZipCode' property on 'Company' could not be set to a 'null' value. You must set this property to a non-null value of type 'System.Int32'.

Can anyone think of any reason why this would be doing that?? I've checked and double checked my database project and my local database, and they both match. And all of my unit tests pass, so I'm pretty much at a loss here.

Any help would be appreciated!

解决方案

Your property in C# Entity Framework is apparently:

public int ZipCode;

You'll have to change that to

public Nullable<int> ZipCode;

You can also do that in the properties window in the visual editor of the entity framework's edmx file.

UPDATE:

It would also be advisable(if possible) to change the type of the ZipCode to string. It would not only take care of the current 'nullable' problem but would also scale very well with any change in requirements further down the line!!

这篇关于我的SQL表被设置为允许此列的NULL,但是当我运行它时,它表示它不能为NULL。什么/为什么/如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆