title: Database computed property that is a foreign key in Entity Framework 6
body: <p>How can I reference a property to be a ForeignKey plugged to a DB computed field (from a view) ?  </p>
  
  <p>I have the following :  </p>
  
  <pre><code>public class PersonSite
  {
      public int Id {get;set;}
  
      //...
  
      public int PersonId {get;set;}
      [ForeignKey("PersonId")]
      public virtual Person Person {get;set;}
  
      //...
  
  }
  
  public class Person
  {
        public Id {get;set;}
  
        //...
  
        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public int? MainPersonSiteId { get; protected internal set; }
  
        [ForeignKey("MainPersonSiteId")]
        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public PersonSite MainPersonSite { get; protected internal set; }
  
        //...
  }
  </code></pre>
  
  <p>Whenever I try to update a <code>Person</code>, I get the following excpetion:</p>
  
  <blockquote>
    <p>Message:</p>
    
    <p>A dependent property in a ReferentialConstraint is mapped to a
    store-generated column. Column: 'MainPersonSiteId'. Stack trace:</p>
    
    <p>at
    System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildSetClauses(DbExpressionBinding
    target, PropagatorResult row, PropagatorResult originalRow,
    TableChangeProcessor processor, Boolean insertMode, Dictionary`2&amp;
    outputIdentifiers, DbExpression&amp; returning, Boolean&amp; rowMustBeTouched)
    at
    System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildUpdateCommand(PropagatorResult
    oldRow, PropagatorResult newRow, TableChangeProcessor processor) at
    System.Data.Entity.Core.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode
    changeNode, UpdateCompiler compiler)</p>
  </blockquote>
  
  <p>In the database, which is Sql Server 12, Person comes from a view, where MainPersonSiteId is projected from a function. This is a computed field, which does not need to be updated by the ORM.  </p>
  
  <p>How to define it with EntityFramework ?  </p>
  
  <p><strong>EDIT :</strong>  </p>
  
  <p>I just managed to have update working by setting <code>DatabaseGeneratedOption.Identity</code> instead of <code>DatabaseGeneratedOption.Computed</code>, nevertheless I just found the inserts were still broken. So I tested the inserts with <code>DatabaseGeneratedOption.Computed</code> back and it ... worked :/</p>
  
  <p>The situation is :<br>
   - I can insert only with DatabaseGeneratedOption.Computed<br>
   - I can update only with DatabaseGeneratedOption.Identity  </p>
  
  <p>It's strange as <a href="https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.databasegeneratedoption%28v=vs.110%29.aspx" rel="nofollow">MSDN says</a> that <code>DatabaseGeneratedOption.Computed</code> let the DB generate a value for both inserts and updates while <code>DatabaseGeneratedOption.Identity</code> does only for inserts</p>