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