简体   繁体   English

带有外键的数据库模型的 C# 8 非空引用类型

[英]C# 8 Non-Null Reference Types for Database models with foreign key

I'am trying to implement non-null-reference-types in my project https://dev.to/integerman/safer-code-with-c-8-non-null-reference-types-4f2c .我正在尝试在我的项目https://dev.to/integerman/safer-code-with-c-8-non-null-reference-types-4f2c 中实现非空引用类型。 I like it, but have a question regarding database models and constraints.我喜欢它,但有一个关于数据库模型和约束的问题。

Is there a way to say that value X is never NULL because it's not nullable in the database?有没有办法说值 X 永远不会为 NULL,因为它在数据库中不可为空?

For example:例如:

public class Person
{
      public int Id { get; set; };

      public string Name { get; set; } = "No name set";

      public IEnumerable<Dog> Dogs { get; set; } = new List<Dog>();
}

Person.Name is nullable=false in the database. Person.Name 在数据库中为 nullable=false。 Is there a way I can say that this property is never null?有没有办法可以说这个属性永远不会为空? Now I have to set a default value.现在我必须设置一个默认值。

public class Dog
{
    public int Id { get; set; }
   
    public string Name { get; set; } = "Noname";

    public int PersonId {get; set; }

    public Person Person { get; set; }
}

Here I wonder the same about Person from Dog.在这里,我想知道关于狗的人也是如此。 This is a foreign key constraint in the database, A Dog can't exist without an Person(owner).这是数据库中的外键约束,没有Person(owner),狗就不能存在。 Is there a way to say that;有没有办法这么说? I know that this value is never null trust me or something like that ?我知道这个值永远不会为空相信我或类似的东西?

[Update] [更新]

Is there a way to say that value X is never NULL because it's not nullable in the database?有没有办法说值 X 永远不会为 NULL,因为它在数据库中不可为空?

Yes, see Pavel Anikhouski answer是的,请参阅 Pavel Anikhouski 的回答

Should you do it:你是否应该这样做:

No, see TomTom s answer不,请参阅 TomTom 的回答

The best solution I think is @canton7 last comment.我认为最好的解决方案是@canton7 最后一条评论。 He links to this;他链接到这个; https://docs.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types#non-nullable-properties-and-initialization https://docs.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types#non-nullable-properties-and-initialization

Another reason:另一个原因:

public IEnumerable<Dog> Dogs { get; set; } = new List<Dog>();

If you are trying to get a Person like this:如果你想得到一个这样的人:

var person = _dbcontext.persons.Single(x => x.Name == "John").ToList(); 
Console.log($"John has {person.Dogs.Count} dogs")

This will output 0 everytime, because we forgot to .Include(x => x.dogs).每次都会输出 0,因为我们忘记了 .Include(x => x.dogs)。 So it's wrong.所以这是错误的。

No, and it makes no sense.不,这没有任何意义。 Particularly in the area of database entities - you MUST allow null because otherwise you can not load the object without automatically loading the related object.特别是在数据库实体领域 - 你必须允许 null 因为否则你不能在不自动加载相关对象的情况下加载对象。 Which ultimately will force you to load a lot of data that you may not want for a simple query.这最终将迫使您加载大量您可能不希望进行简单查询的数据。

Dogs CAN exist without Person.狗可以没有人而存在。 Not on db level, but I can ask for a list of all dogs and not be interested in the owners at this point.不是在 db 级别,但我可以要求所有狗的清单,此时对主人不感兴趣。

You can use null forgiving operator !您可以使用空原谅运算符! for that为了那个原因

public Person Person { get; set; } = default!;

As it pointed in comments, you should carefully use it, since it works with any type (value or reference one) and you can use null!正如评论中指出的那样,您应该谨慎使用它,因为它适用于任何类型(值或引用),并且您可以使用null! as well还有

暂无
暂无

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

相关问题 为什么我会收到这些奇怪的 C# 8 “可空引用类型”警告:在退出构造函数之前,不可空字段必须包含非空值。? - Why am I getting these strange C# 8 “nullable reference types” warnings: Non-nullable field must contain a non-null value before exiting constructor.? C#中的非空类默认 - Non-null class default in C# 无法启用约束。 一或多个行包含违反非null,唯一或外键约束的值。 ASP.NET C# - Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. asp.net c# C#SQL:无法启用约束。 一或多个行包含违反非空,唯一或外键约束的值 - C# SQL: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints 将C#非空参数附加到字符串 - C# non-null parameters to be appended to a string C# 中是否有“true or throw”或“non-null or throw”表达式? - Is there a "true or throw" or "non-null or throw" expression in C#? C#泛型:成员的默认非空值 - C# generics: default non-null value for member C# 将引用和值类型与 null 进行比较 - C# comparing reference and value types with null C#List,删除* last非null元素后的所有空值* - C# List, Removing all null values *after* last non-null element 为什么C#在转换后将实例的先前非null成员视为null? - Why does C# consider a previously non-null member of an instance as null after an upcast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM