title: Entity Framework - Entity read-only property mapped to a column of related table
body: <p>I have interesting problem to solve but, although common, it looks like it's not easily achievable with Entity Framework. There are two tables:</p>
  
  <pre><code>Player(Id,TeamId,FirstName,LastName)
  Team(Id, Name, IsProfessional)
  </code></pre>
  
  <p>Player can belong only to one team. Using TPT (DB first), we have two classes mapped to those tables:</p>
  
  <pre><code>public class Player
  {
     public int Id{get;set;}
     public int TeamId{get;set;}
     public string FirstName{get; set;}
     public string LastName{get; set;}
     public Team Team{get;set;}
  }
  
  public class Team
  { 
     public int Id{get; set;}
     public string Name{get;set;}
     public bool IsProfessional{get;set;}
     public IEnumerable&lt;Player&gt; Players{get;}
  }
  </code></pre>
  
  <p>What I would like to achieve is property IsProfessional on Player entity:</p>
  
  <pre><code>public class Player
      {
         public int Id{get;set;}
         public int TeamId{get;set;}
         public string FirstName{get; set;}
         public string LastName{get; set;}
         public Team Team{get;set;}
         **public bool IsProfessional{get;}** should be read-only
      }
  </code></pre>
  
  <p>Is it possible to configure mapping that way IsProfessional property can be used in linq queries?</p>
  
  <pre><code>var result= db.Players.Where(p=&gt;p.IsProfessional==true);
  </code></pre>
  
  <p>and to have that field populated every time Player entity is materialized?</p>
  
  <pre><code>Player pl = db.Players.Where(p=&gt;p.FirstName="Lionel").FirstOrDefault();
  if(pl.IsProfessional)
  {
  //do something...
  }
  </code></pre>
  
  <p>Already tried with:</p>
  
  <ul>
  <li><strong>Entity Splitting</strong>. Not possible because I want to keep Team mapping and because relationship is not 1:1)</li>
  <li><strong>Mapping Player entity to a db view</strong>. Didn't like it because there are other relationships Player entity has that I need. I know it is possible to create them manually, but updating edmx from database will reset ssdl.</li>
  </ul>
  
  <p>Thanks</p>
  
  <p><strong>Solution</strong></p>
  
  <p>Based on second option in Gert Arnold answer, solution that fits my needs is as follows:</p>
  
  <ol>
  <li><p>I create function <code>GetIsProfessional</code> (had to do it because computed fields normally can be made only from own table fields)</p>
  
  <pre><code>CREATE FUNCTION [dbo].[GetIsProfessional](@teamId as INT)
  RETURNS bit
  
  BEGIN
  
  DECLARE @isProfi AS bit
  
  SELECT @isProfi = IsProfessional
  FROM Teams
  WHERE Id = @teamId
  
  RETURN @isProfi
  
  END
  </code></pre></li>
  <li><p>I created computed field on <code>Player</code> table</p>
  
  <pre><code>ALTER TABLE Players ADD [IsProfessional] AS dbo.GetIsProfessional(TeamId)
  </code></pre></li>
  <li><p>As I'm using db first approach, I just update model from database and that's it, I can query on that field and it's pre populated when Player object is materialized.</p></li>
  </ol>