Home > database >  dotnet6 entity framework where clause
dotnet6 entity framework where clause

Time:11-17

i have converted by project from dotnet core 2.1 to dotnet 6. i am in the process of converting it step by step. the thing which i am stuck now is where clause. i am not able to run this simple query in repository

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

anyone can get why? queries are working before

this is the result

enter image description here

Posttype

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

enter image description here

CodePudding user response:

I would argue that this issue is similar to this one and caused by addition of nullable reference types and their support in EF Core. See the Required and optional properties -> Conventions section of the docs:

C# 8 introduced a new feature called nullable reference types (NRT), which allows reference types to be annotated, indicating whether it is valid for them to contain null or not. This feature is enabled by default in new project templates, but remains disabled in existing projects unless explicitly opted into. Nullable reference types affect EF Core's behavior in the following way:

  • If nullable reference types are disabled, all properties with .NET reference types are configured as optional by convention (for example, string).
  • If nullable reference types are enabled, properties will be configured based on the C# nullability of their .NET type: string? will be configured as optional, but string will be configured as required.

Either disable nullable reference types for the project or mark corresponding properties as nullable (in this case some string causes the issue, so change string to string? for corresponding properties).

  • Related