本文介绍了如何在 Entity Framework Core 模型中使用 C# 8.0 可空引用类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 .NET Core 3.0 项目上启用 C# 8.0 可空引用类型.项目使用Entity Framework Core 3.0访问数据库.

I am enabling C# 8.0 Nullable Reference Types on a .NET Core 3.0 project. The project uses Entity Framework Core 3.0 to access database.

以下是 Title 不能为空的数据模型.

The following is a data model whose Title should not be null.

public class Vehicle
{
    public int Id { get; private set; }

    public string Title { get; private set; }

    // Entity Framework Core is instructed to bind to the private _drivers field in a configuration builder
    private readonly List<Driver> _drivers = new List<Driver>();
    public IReadOnlyCollection<Driver> Drivers => _drivers.AsReadOnly();

    private Vehicle()
    {
    }

    public Vehicle(string title)
    {
        this.Title = title;
    }

    public void AddDriver(string name)
    {
         this._drivers.Add(new Driver(name));
    }
 }

// A foreign column is defined in a configuration builder
public class Driver
{
    public int Id { get; private set; }

    public string Name { get; private set; }

    private Driver()
    {
    }

    public Driver(string name)
    {
        this.Name = name;
    }
 }

自己的代码应该只使用 public 构造函数,而 private 构造函数只是为了允许 Entity Framework Core 和(可能也是)序列化来绑定来自数据库的值到这些类/模型.公共构造函数可能具有与模型具有的属性不同的结构、列表和参数类型(例如,它可能还包含第一个必需子项的参数,它可能有一些可选参数等).

Own code is supposed to use the public constructors only while the private constructors are there just to allow Entity Framework Core and (potentially also) serialization to bind values from database to these classes/models. The public constructor might have different structure, list and types of arguments than which properties the model has (for example, it might also contains arguments for the first required child, it might have some arguments optional etc.).

但是,编译器生成 CS8618 Non-nullable 字段未初始化.考虑在 private 构造函数上声明为可空..

However, the compiler generates CS8618 Non-nullable field is uninitialized. Consider declaring as nullable. on the private constructors.

我可以通过 #pragma warning disable CS8618private 构造函数禁用 CS8616,但我认为这不是一个好主意.

I am able to disable CS8616 for the private constructors by #pragma warning disable CS8618 but I do not consider that as a good idea.

在这种情况下应该如何使用 C# 8.0 可空引用类型?或者我的模型是假的或违反了最佳实践 - 如何正确地做?

How it is supposed to use C# 8.0 Nullable Reference Types in this scenario?Or is my model bogus or violates best practices - how to do it properly?

很遗憾,我没有找到相关的文档或指南.

Unfortunatelly, I have found not relevant docs or guidance.

推荐答案

没有正确的方法来处理不可为 null 的导航属性.

There is no proper way to handle non-nullable navigational properties.

  1. 文档提出了两种方法,但都不是类型安全的.用一个支持字段并抛出 InvalidOperationException.不清楚如何它不同于什么都不做并且有一个 NullReferenceException
  2. 用空原谅运算符抑制它

官方文档链接:https://docs.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types#non-nullable-properties-and-initialization

这篇关于如何在 Entity Framework Core 模型中使用 C# 8.0 可空引用类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:46