title: Check if a property of an Entity is marked as IsRequired() via reflections and EF Core
body: <p><strong>THE QUESTION</strong></p>
  
  <p>If I have an array of properties of a certain Entity and I'm iterating through them, <strong>is there any way to check if the reflected type property that I am iterating in each cycle is configured as <code>.IsRequired()</code></strong> on its corresponding Entity?</p>
  
  <p><strong>EXAMPLE</strong></p>
  
  <p>This question has to be intended especially for <code>string</code> properties, as in most of value types, if a db property allows <code>null</code> values, then it is mapped by EF Core's Scaffolding operation as nullable type.</p>
  
  <p>E.G.: a nullable int is mapped as <code>int?</code>, while a not-nullable one is mapped as <code>int</code>.</p>
  
  <p>If I iterate through that mapped Entity's properties, to check if that property I am iterating now is a nullable one, I just have to check if <code>myproperty.PropertyType == typeof(int?)</code> but...what in case of a <code>string</code> type?</p>
  
  <p>Is there any way to check if it's marked as an <code>.IsRequired()</code> property?</p>
  
  <p><strong>MY CODE SO FAR</strong></p>
  
  <p>In my code I have the following function, which is supposed to receive as params:</p>
  
  <ul>
  <li><code>objectInstance</code>: a Proxy derived from the Entity that I have to update, that I (have to) find previously</li>
  <li><code>values</code>: a dictionary with the property name and the new value of the properties I have to update. It could be filled with every property, or just with some of them.</li>
  <li><code>properties</code>: the array of properties of the class I previously found via reflections</li>
  </ul>
  
  <p>This function is supposed to iterate through the properties array and for each property, if the new value is contained inside the dictionary, to set its new value on the instance of the class.</p>
  
  <pre><code>private static bool SetValues(Object objectInstance, Dictionary&lt;string, object&gt; values, PropertyInfo[] properties)
  {
      bool edited = false;
      foreach (var item in values)
      {
          var temp = properties.Where(w =&gt; w.Name.ToLower() == item.Key.ToLower()).FirstOrDefault();
          if (temp != null)
          {
              edited = true;
              if (temp.PropertyType == typeof(string))
              {
                  //here it is where I would like to do the above mentioned check
                  temp.SetValue(objectInstance, Convert.ToString(item.Value));
              }
              if (temp.PropertyType == typeof(int) || temp.PropertyType == typeof(int?))
              {
                  temp.SetValue(objectInstance, Convert.ToInt32(item.Value));
              }
              if (temp.PropertyType == typeof(long) || temp.PropertyType == typeof(long?))
              {
                  temp.SetValue(objectInstance, Convert.ToInt64(item.Value));
              }
              if (temp.PropertyType == typeof(decimal) || temp.PropertyType == typeof(decimal?))
              {
                  temp.SetValue(objectInstance, Convert.ToDecimal(item.Value));
              }
              if (temp.PropertyType == typeof(bool) || temp.PropertyType == typeof(bool?))
              {
                  temp.SetValue(objectInstance, Convert.ToBoolean(item.Value));
              }
              if (temp.PropertyType == typeof(DateTime) || temp.PropertyType == typeof(DateTime?))
              {
                  temp.SetValue(objectInstance, Convert.ToDateTime(item.Value));
              }
          }
      }
      return edited;
  }
  </code></pre>
  
  <p>Here's how I get "objectInstance":</p>
  
  <pre><code>var objectInstance = _context.Query(TableType).Where("Id = @0", rowKey).FirstOrDefault();
  </code></pre>
  
  <p>Where "Query" is an Extension:</p>
  
  <pre><code>public static IQueryable Query(this DbContext context, Type entityType) =&gt;
              (IQueryable)((IDbSetCache)context).GetOrAddSet(context.GetDependencies().SetSource, entityType);
  </code></pre>
  
  <p>And... an example of what I mean with a <code>IsRequired()</code>-marked property of an Entity, to avoid misunderstandings:</p>
  
  <pre><code> public void Configure(EntityTypeBuilder&lt;MyTable&gt; builder)
   {
      //[a lot of properties above here...]
      builder.Property(e =&gt; e.Code)
                      .IsRequired()  //that's it!
                      .HasMaxLength(50)
                      .IsUnicode(false);
      //...
   }
  </code></pre>
  
  <p><strong>WHAT I WOULD LIKE TO ACHIEVE</strong></p>
  
  <p>On the <code>//here it is where I would like to do the above mentioned check</code> comment's position, I would like to check if (pseudocode):</p>
  
  <pre><code>if(temp.IsRequired())
  {
     if(String.IsNullOrWhiteSpace(Convert.ToString(item.Value)))
     {
         temp.SetValue(objectInstance, "");
     }
     else
     {
         temp.SetValue(objectInstance, Convert.ToString(item.Value));
     }
  }
  else
  {
     if(String.IsNullOrWhiteSpace(Convert.ToString(item.Value)))
     {
         temp.SetValue(objectInstance, null);
     }
     else
     {
         temp.SetValue(objectInstance, Convert.ToString(item.Value));
     }
  }
  </code></pre>