title: ASP.NET MVC reads properties when object created
body: <p>I have noticed the following behavior and I wonder if anyone can explain why it is happening and how it can be prevented.</p>
  
  <p>In my partial classes I have added various read-only properties that are not present in the database.  When the object is created, these properties are accessed before we get to the controller's Create method.  However, properties declared with just <code>get; set;</code> are not accessed.</p>
  
  <p>To elaborate, using very simple examples, if I have the following properties:</p>
  
  <pre><code>public bool getBoolProperty {
      get {
          return true;
      }
  }
  public bool getSetBoolProperty {
      get; set; 
  }
  </code></pre>
  
  <p>If I then put breakpoints on the properties, when the object is created the breakpoint is hit for the first property but not the second.  However, if I have a method:</p>
  
  <pre><code>public bool getBoolProperty() {
      return true;
  }
  </code></pre>
  
  <p>then this is not accessed.</p>
  
  <p>I have tried all sorts of variations and annotations</p>
  
  <pre><code>// empty set
  public bool getBoolProperty {
      get {
          return true;
      }
      set {}
  }
  
  // not mapped attribute
  [NotMapped]
  public bool getBoolProperty {
      get {
          return true;
      }
  }
  
  // getting private variable
  private bool _boolProperty = true;
  public bool getPrivateBoolProperty {
      get {
          return _boolProperty;
      }
  }
  </code></pre>
  
  <p>I've tried declaring the properties <code>virtual</code>, yet all variations, except the <code>get; set;</code> variety, are accessed when the object gets created.  This behaviour also occurs for integer properties</p>
  
  <pre><code>public virtual int getIntProperty {
      get {
          return 1;
      }
  }
  </code></pre>
  
  <p>and date/time properties, </p>
  
  <pre><code>public virtual DateTime getDateProperty {
      get {
          return DateTime.Now;
      }
  }
  </code></pre>
  
  <p>but not for string properties</p>
  
  <pre><code>public string getStringProperty {
      get {
          return "Hello String";
      }
  }
  </code></pre>
  
  <p>or other entity properties</p>
  
  <pre><code>public Item getItem {
      get {
          return new Item();
      }
  }
  </code></pre>
  
  <p>The problem I have is that some of these properties can involve some logic that might need database access eg</p>
  
  <pre><code>public bool HasChildren {
      get {
          return this.Children !== null &amp;&amp; this.Children.Count &gt; 0;
      }
  }
  </code></pre>
  
  <p>which, at the time of creation, the entity won't have.</p>
  
  <p>Obviously I can get around this by making everything a method, but I would be interested to know why ASP.NET MVC accesses these properties on object creation (Some sort of internal validation perhaps), and I would be interested to know if there are any methods of preventing this, possibly by way of an annotation I haven't come across.</p>
  
  <p>My project is database first, C#, EF 4.1, MVC</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I should also point out I'm using POCO entities and accessing the database through stored procedures/function imports.</p>
  
  <p>One thought occurred to me is that these  properties are being accessed as part of the change tracking system as discussed <a href="http://msdn.microsoft.com/en-gb/library/vstudio/dd456848%28v=vs.100%29.aspx" rel="nofollow noreferrer">here</a>.  It seems that what is probably happening is a snapshot is being taken of the newly created item.
  I tried turning off proxy creation </p>
  
  <pre><code>dbContext.ContextOptions.ProxyCreationEnabled = false;
  </code></pre>
  
  <p>but the properties are still getting accessed on creation.</p>
  
  <p><strong>Edit 20130322</strong></p>
  
  <p>Following @ladislavmrnka's suggestion of exploring the stack trace I got this:</p>
  
  <pre><code>at System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component)
  at System.Web.Mvc.AssociatedMetadataProvider.&lt;&gt;c__DisplayClassb.&lt;GetPropertyValueAccessor&gt;b__a()
  at System.Web.Mvc.ModelMetadata.get_Model()
  at System.Web.Mvc.DataAnnotationsModelValidator.&lt;Validate&gt;d__1.MoveNext()
  at System.Web.Mvc.ModelValidator.CompositeModelValidator.&lt;Validate&gt;d__5.MoveNext()
  at System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
  at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model)
  at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
  at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
  at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
  at System.Web.Mvc.Controller.ExecuteCore()
  at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
  at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
  at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClass6.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5()
  at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0()
  at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _)
  at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
  at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d()
  at System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f)
  at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
  at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
  at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>In it you will notice the calls to a validator or two.  As a test I changed my bool property to a nullable bool property:</p>
  
  <pre><code>public bool? getBoolProperty {
      get {
          return true;
      }
  }
  </code></pre>
  
  <p>This time the property was not accessed when the object was created.  This is my desired behaviour, however, I don't want to have to change all my custom properties to be nullable, so my question now becomes...</p>
  
  <p><strong>Is there any way to tell the framework not to validate a property?</strong>  Perhaps via an attribute.  <a href="https://stackoverflow.com/a/7056192/1021958">This answer</a> almost answers the question, but since I am using database first, I don't seem to have a <code>ValidateOnSaveEnabled</code> property to turn off.  Perhaps I need to revisit my models.</p>