C# 使用空模型

C# 使用空模型,c#,asp.net-mvc,C#,Asp.net Mvc,我有两门课: 具有某些属性的库 具有一些属性和一个库的位置。 我希望有或没有一个画廊的位置,所以我喜欢这样: public Gallery? Gallery { get; set; } 问题是我遇到了以下错误: Error 3 The type 'Gallery' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Null

我有两门课:
具有某些属性的库
具有一些属性和一个库的位置。
我希望有或没有一个画廊的位置,所以我喜欢这样:

public Gallery? Gallery { get; set; }
问题是我遇到了以下错误:

Error   3   The type 'Gallery' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
错误3类型“Gallery”必须是不可为null的值类型,才能将其用作泛型类型或方法“System.nullable”中的参数“T”
我一直在寻找这个错误,但我找不到任何解决方案(至少我理解)。我的问题是如何解决这种情况

公众席?库{get;set;}

这意味着:

  • Gallery
    是一种不能为null的类型
  • Gallery
    属性可以是
    Gallery
    null
  • 第一个不是真的。因此:

  • 通过将
    class Gallery
    更改为
    struct Gallery
    ,更改
    Gallery
    ,使其不能为空
  • 将属性更改为
    public Gallery{get;set;}
    ,以使用当前定义的
    Gallery
    可以为空的事实

  • 让我猜猜:
    Gallery
    是一个类吗?Gallery(大概)是一个类,它自然可以为null,不能与
    一起使用,它是结构的装饰。如果
    在您的
    位置
    型号上是可选的,请使用可为空的id属性作为外键指示。例如,
    public int?GalleryId{get;set;}
    为什么要使用“Gallery?”而Gallery是一个类,因此可以为空类型?@AnthonyPegram:您可以在ORM层中将
    Gallery
    定义为可选,不是吗?这样您就可以保留导航属性。@Jeroenvanevel,Id属性是导航属性之外的属性。你可以定义两者。