简体   繁体   English

EF Core fluent API 的可空警告

[英]Nullable warning with EF Core fluent API

I have nullable enabled in VS2022.我在 VS2022 中启用了可为空。

I don't see what the valid resolution to this nullable warning is.我看不出这个可空警告的有效解决方案是什么。

builder.OwnsOne(o => o.CreatedByUser, createdByUser => {
createdByUser.Property(userRef => userRef.UserId)
  .HasColumnName("CreatedByUser");
});

在此处输入图像描述

Changing改变

userRef.UserId userRef.UserId

to

userRef?.UserId userRef?.UserId

would clear the error, but it's a fluent API definition, and that would make no sense (I believe).会清除错误,但这是一个流畅的 API 定义,这没有任何意义(我相信)。

I could add warning suppression, but that's equally rubbish.我可以添加警告抑制,但这同样是垃圾。

What would be an appropriate resolution to this warning, please?请问这个警告的适当解决方案是什么?

Many fluent (and other) APIs in pre EFC 6.0 are not NRT annotated, hence you have to suppress NRT warnings or use null forgiving tricks. EFC 6.0 之前的许多流畅(和其他)API 没有 NRT 注释,因此您必须禁止 NRT 警告或使用 null 宽恕技巧。

This has been fixed in EFC 6.0, Reference: What's New in EF Core 6.0 - EF Core annotated for C# nullable reference types :这已在 EFC 6.0 中修复,参考: EF Core 6.0 中的新增功能- EF Core 为 C# 可空引用类型注释

GitHub Issue: #19007 . GitHub 问题: #19007

The EF Core codebase now uses C# nullable reference types (NRTs) throughout. EF Core 代码库现在始终使用C# 可空引用类型 (NRT) This means that you will get the correct compiler indications for null usage when using EF Core 6.0 from your own code.这意味着在您自己的代码中使用 EF Core 6.0 时,您将获得 null 使用的正确编译器指示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM