0

I have two classes:
Gallery with some attributes
Location with some attributes and one gallery.
I would like to have or not a gallery in location, so I did like this:

public Gallery? Gallery { get; set; }

The problem is that I'm getting this error:

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>'

I've been looking for this error, but I could't find any solution (at least that I understood). My question is how can I fix this situation?

6
  • 1
    Let me guess: Gallery is a class? Commented Jun 7, 2015 at 22:36
  • 2
    Gallery is (presumably) a class, which is naturally nullable and can't be used with ?, which is a decoration for a struct. If Gallery is optional on your Location model, indicate that with a nullable id property as the foreign key. For example, public int? GalleryId { get; set; } Commented Jun 7, 2015 at 22:36
  • Why do you use "Gallery?" while Gallery is a class, thus nullable type? Commented Jun 7, 2015 at 22:37
  • @AnthonyPegram: you could just define Gallery as optional in the ORM layer, no? That way you can keep the navigational property. Commented Jun 7, 2015 at 22:39
  • @JeroenVannevel, the Id property is in addition to the navigation property. You would define both. Commented Jun 7, 2015 at 22:48

1 Answer 1

2

public Gallery? Gallery { get; set; }

This means:

  1. Gallery is a type that cannot be null.
  2. The Gallery property can either be a Gallery or null.

The first of these isn't true. Therefore either:

  1. Change Gallery so that it can't be null by changing class Gallery to struct Gallery.
  2. Change the property to public Gallery Gallery { get; set; } to use the fact that Gallery as currently defined can already be null.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.