我已經按專案從 dotnet core 2.1 轉換為 dotnet 6。我正在逐步轉換它。我現在卡住的是 where 子句。我無法在存盤庫中運行這個簡單的查詢
public IQueryable<PostType> getPostType()
{
var d= _context.PostType.Where(e => e.PostStatusId==2).ToList(); // this thing is not working
return _context.PostType.Where(e => e.PostStatusId == 2);
}
誰能明白為什么?查詢之前有效
這是結果

郵政型別
CREATE TABLE [dbo].[PostType](
[PostTypeId] [int] NOT NULL,
[PostTypeGuid] [uniqueidentifier] NULL,
[ParentId] [int] NULL,
[CategoryId] [int] NULL,
[Name] [varchar](100) NOT NULL,
[Slug] [varchar](500) NULL,
[ThumbnailImg] [nvarchar](255) NULL,
[WebSiteId] [int] NULL,
[PostStatusId] [int] NOT NULL,
[isMediaType] [bit] NOT NULL,
[isNewsletter] [bit] NOT NULL,
[AllowableExtension] [varchar](100) NULL,
[CreatedBy] [int] NULL,
[CreatedDate] [datetime] NULL,
[ModifiedBy] [int] NULL,
[ModifiedDate] [datetime] NULL,
CONSTRAINT [PK_PostType] PRIMARY KEY CLUSTERED
(
[PostTypeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PostType] ADD CONSTRAINT [DF_PostType_PostTypeGuid] DEFAULT (newid()) FOR [PostTypeGuid]
GO
ALTER TABLE [dbo].[PostType] ADD CONSTRAINT [DF_PostType_int_PostStatusId] DEFAULT ((2)) FOR [PostStatusId]
GO

uj5u.com熱心網友回復:
我認為這個問題與這個問題類似,是由添加可為null 的參考型別及其在 EF Core 中的支持引起的。請參閱檔案的Required and optional properties -> Conventions部分:
C# 8 引入了一個稱為可空參考型別 (NRT) 的新功能,它允許對參考型別進行注釋,指示它們是否有效包含 null。此功能在新專案模板中默認啟用,但在現有專案中保持禁用狀態,除非明確選擇加入。可空參考型別通過以下方式影響 EF Core 的行為:
- 如果禁用可為 null 的參考型別,則所有具有 .NET 參考型別的屬性都按約定配置為可選(例如,字串)。
- 如果啟用可為空的參考型別,將根據其 .NET 型別的 C# 可空性配置屬性:字串?將配置為可選,但字串將根據需要配置。
為專案禁用可為 null 的參考型別或將相應的屬性標記為可為 null(在這種情況下,某些string會導致問題,因此請更改string為string?相應的屬性)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/535924.html
