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 ),
});