This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3654|Reply: 0

[Source] EF SQL statements automatically generate IS NULL or IS NOT NULL

[Copy link]
Posted on 6/21/2023 10:42:18 PM | | | |
Requirements: When EF uses linq to sql or lambda expressions to query the database, the SQL statements generated by EF will help us add some IS NULL or NOT NULL conditions.

Example 1:

It generates a SQL statement like this:

SELECT
    [Extent2]. [UserStatus] AS [UserStatus],
    [Extent1]. [Id] AS [Id]
    FROM  [dbo]. [EmailInfo] AS [Extent1]
    INNER JOIN [dbo]. [Account] AS [Extent2] ON ([Extent1].[ Address] = [Extent2]. [Email]) OR (([Extent1].[ Address] IS NULL) AND ([Extent2].[ Email] IS NULL))



Example 2:

It generates a SQL statement like this:

SELECT
    [Extent1]. [Id] AS [Id],
    [Extent1]. [Name] AS [Name],
    [Extent1]. [ParentId] AS [ParentId],
    [Extent1]. [Position] AS [Position],
    [Extent1]. [_CreateTime] AS [_CreateTime],
    [Extent1]. [_UpdateTime] AS [_UpdateTime]
    FROM [dbo]. [Classification] AS [Extent1]
    WHERE ([Extent1].[ Name] IN (N'Android', N'Solaris')) AND ([Extent1].[ Name] IS NOT NULL)



If you want to avoid EF generating these additional NULL conditions, you can refer to the following.

Configure the DbContextConfiguration.UseDatabaseNullSemantics property

Gets or sets a value that indicates whether to display database null semantics when comparing two operands and they are both likely to be null. The default value is false.

The code is as follows:


For example:
If UseDatabaseNullSemantics is true, then (operand1 == operand2) will be converted to (operand1 = operand2);
If UseDatabaseNullSemantics is false, it will be converted to (((operand1 = operand2) AND (NOT (operand1 IS NULL OR operand2 IS NULL))) OR ((operand1 IS NULL) AND (operand2 IS NULL))).

Documentation (There is an error in the documentation description):The hyperlink login is visible.

field to add the [Required] attribute

After EF adds the [Required] attribute to the object property, it actually does not allow the attribute (field) to be NULL in the database.After executing the migration command, the field is not allowed to be NULLSince the field is not allowed to be NULL in the database, EF naturally does not generate some NULL checks in the SQL statements generated for that field.

Reference:The hyperlink login is visible.






Previous:ASP.NET Core (twenty-one) configuration options are the difference between AddOptions and Configure
Next:The ajax-hook for web development intercepts all XMLHttpRequest requests
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com