0

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

2
  • "is not working" is not very helpful. How is it not working? Exception? Returns empty result? Commented Nov 17, 2022 at 14:06
  • Show us the definition of class PostType , and show us your CREATE TABLE statements. Commented Nov 17, 2022 at 14:10

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

11 Comments

It looks like Slug, ThumbnailImg and AllowableExtension all need to be changed from string to string? if nullable reference types are not disabled.
@jmcilhinney yes
it doesn't work by changing these three properties. corresponding foreign tables are linked needed to be changed as well?
@maztt exception changes or is the same?
it works by disabling
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.