c#-不能将空值分配给类型为System.Boolean的成员,这是非空值类型

select new
{
    countFair = (pstvte.Fair),
});

我已将“公平”列的默认值设置为“ false”.现在,我在新字段中分配了这个非空布尔值,它给出了以下错误:

The null value cannot be assigned to a member with type System.Boolean which is a non-nullable value type

CREATE TABLE [dbo].[PostVote](
    [PostVoteId] [bigint] IDENTITY(1,1) NOT NULL,
    [PostId] [bigint] NOT NULL,
    [UserId] [bigint] NOT NULL,
    [Fair] [bit] NOT NULL,
    [NotFair] [bit] NOT NULL,
 CONSTRAINT [PK_PostVote] PRIMARY KEY CLUSTERED 
(
    [PostVoteId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[PostVote] ADD  DEFAULT ('False') FOR [Fair]
GO

ALTER TABLE [dbo].[PostVote] ADD  DEFAULT ('False') FOR [NotFair]
GO

解决方法:

我认为您需要更改countFair的声明

bool countFair;

bool? countFair;

要么

做这样的事情

select new
{
    countFair = (pstvte.Fair.HasValue ? pstvte.Fair.Value : false ),
});
上一篇:python – HSV到RGB颜色转换


下一篇:学习Spring-Data-Jpa(十三)---动态查询接口JpaSpecificationExecutor