我正在尝试在我的项目中包含 C# 8 的可为空引用类型,并使其与 EF Core 顺利工作。
按照本指南,我使我的实体类具有接受初始化其不可为空属性所需的所有数据的构造函数:
public class MyEntity
{
public MyEntity(int someNumber, string name, string shortName, bool active)
{
SomeNumber= someNumber;
Name = name;
ShortName = shortName;
Active = active;
}
public int SomeNumber { get; set; }
public string Name { get; set; }
public string ShortName { get; set; }
public string? SomethingOptional { get; set; }
public bool Active { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的业务案例中,我有时需要更新实体的所有属性。我可以使用属性设置器,但由于我想通过将初始化语法加倍来确保不会遗漏任何内容(实际上我的实体可以有 10 个或更多属性),我决定创建一个公共 Update() 函数以方便使用并调用它而不是构造函数体:
public MyEntity(int someId, …Run Code Online (Sandbox Code Playgroud) c# constructor entity-framework-core c#-8.0 nullable-reference-types
当我尝试使用属性参数时,我刚刚收到此typeof(string?)错误SwaggerResponse。
是The typeof operator cannot be used on a nullable reference type在编译器里的
和The typeof expression cannot be used on nullable reference types《骑士》中。
为什么我不能这样做?当我需要在编译时强调类型可为空时(如所描述的问题),我该怎么办?
我正在研究包含默认 Unity 项目 ( Assembly-CSharp )的解决方案。
我在解决方案中添加了一个测试项目,并引用了Assembly-CSharp。根据 IntelliSense,测试项目确实看到了来自Assembly-CSharp的类型,但是当我尝试构建解决方案时,我收到以下错误:
Metadata file 'MyProjectFolder\Temp\bin\Debug\Assembly-CSharp.dll' could not be found.
因此,我无法在我的 Unity 项目上运行单元测试。
实际上,当我构建 Unity 项目时,.\Temp\bin\Debug即使 VS 认为构建成功,该文件夹仍然是空的。