title: EntityFramework Core Computed Property Error: InvalidOperation No Backing field
body: <p>I'm currently making a web app using .net core 2.2 and EntityFramework.</p>
  
  <p>I have a model which has 2 computed properties "AgeYears" and "AgeMonths" these properties are calculated based on the property "Birthday" by a class library.</p>
  
  <p>When I run the web app, and there is a fetch which includes this model, an error is thrown:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: No backing field could be found for
    property 'AgeMonths' of entity type 'HorseModel' and the property does
    not have a setter.</p>
  </blockquote>
  
  <p>I've tried to figure out what's going on by reading through a couple of articles
  one of which is on the Microsoft website but just returns a simple concatenation of FirstName and LastName (FullName) and the other didn't seem to be up to date as there were attributes that simply were not available e.g. [computed]. Usually, I wouldn't bother with the properties, and simply do the calculation in the view or page model, but stupidly thought this would be a better/cleaner idea!?</p>
  
  <p>Here's the part of the model causing problems: </p>
  
  <pre><code>    public DateTime Birthday { get; set; }
      [NotMapped]
      public int AgeYears
      {
          get
          {
              return DateTimeSpan.CompareDates(DateTime.Now, Birthday).Years;
          }
      }
      [NotMapped]
      public int AgeMonths
      {
          get
          {
              return DateTimeSpan.CompareDates(DateTime.Now, Birthday).Months;
          }
      }
  </code></pre>
  
  <p>I tried deleting the database and migrations, and upon attempting to run the initial migration again the error showed up, despite adding [NotMapped] to the properties.</p>
  
  <p>What I was expecting to happen, is that when a record is fetched from the database, these properties are calculated and thought there would be no need to store them in the database.  Clearly, I'm doing something wrong, and I assume its related to using my own functions for the calculation.</p>
  
  <p>To make it work I've added a backing field as the error suggested I do, and when I look at the migration, I notice there are still columns being generated for the "NotMapped" values.</p>
  
  <pre><code>        migrationBuilder.CreateTable(
              name: "Horses",
              columns: table =&gt; new
              {
                  Id = table.Column&lt;int&gt;(nullable: false)
                      .Annotation("Sqlite:Autoincrement", true),
                  Name = table.Column&lt;string&gt;(nullable: true),
                  Birthday = table.Column&lt;DateTime&gt;(nullable: false),
                  AgeYears = table.Column&lt;int&gt;(nullable: false),
                  AgeMonths = table.Column&lt;int&gt;(nullable: false),
                  Breed = table.Column&lt;string&gt;(nullable: true),
                  Height = table.Column&lt;decimal&gt;(nullable: false),
                  About = table.Column&lt;string&gt;(nullable: true),
                  ForSale = table.Column&lt;bool&gt;(nullable: false),
                  Private = table.Column&lt;bool&gt;(nullable: false),
                  OwnerId = table.Column&lt;string&gt;(nullable: true),
                  Created = table.Column&lt;DateTime&gt;(nullable: false),
                  LastEdited = table.Column&lt;DateTime&gt;(nullable: false),
                  PurhaseDate = table.Column&lt;DateTime&gt;(nullable: false),
                  Sold = table.Column&lt;DateTime&gt;(nullable: false),
                  Slug = table.Column&lt;string&gt;(nullable: true),
                  OwnershipStatus = table.Column&lt;int&gt;(nullable: false)
              },...
  </code></pre>
  
  <p>Now however AgeYears and AgeMonths don't get calculated when they are fetched!</p>
  
  <p>The final model that I came up with is:</p>
  
  <pre><code>public class HorseModel
  {
      private int _ageYears;
      private int _ageMonths;
  
      public int Id { get; set; }
      public string Name { get; set; }
      public DateTime Birthday { get; set; }
      [NotMapped]
      public int AgeYears
      {
          get
          {
              return _ageYears;
          }
          set
          {
              _ageYears = DateTimeSpan.CompareDates(DateTime.Now, Birthday).Years;
          }
      }
      [NotMapped]
      public int AgeMonths
      {
          get
          {
              return _ageMonths;
          }
          set
          {
              _ageMonths = DateTimeSpan.CompareDates(DateTime.Now, Birthday).Months;
          }
      }
      public string Breed { get; set; }
      public decimal Height { get; set; }
      public string About { get; set; }
      public bool ForSale { get; set; }
      public bool Private { get; set; }
      [ForeignKey("Owner")]
      public string OwnerId { get; set; }
      public ApplicationUser Owner { get; set; }
      public List&lt;HorsePicture&gt; Pictures { get; set; }
      public List&lt;HorseYouTubeVideo&gt; Videos { get; set; }
      public DateTime Created { get; set; }
      public DateTime LastEdited { get; set; }
      public DateTime PurhaseDate { get; set; }
      public DateTime Sold { get; set; }
      public HorseProfilePicture ProfilePicture { get; set; }
      public string Slug { get; set; }
      public OwnershipStatus OwnershipStatus { get; set; }
  }
  </code></pre>
  
  <p>Any guidance would be very much appreciated.</p>
  
  <p>Thanks :)</p>