title: How to Pass model data to layout page in mvc with Entity Framework?
body: <p>I am stuck in a problem where I want to show model's properties on layout page. With the reference of <a href="https://stackoverflow.com/a/13231752/3756000">this answer</a> I tried to implement the model as the following way</p>
  
  <blockquote>
    <p>CompanyProfile Model</p>
  </blockquote>
  
  <pre><code>[Table("tblCompany")]
      public abstract class CompanyProfile
      {
          [Key]
          public int CompanyId { get; set; }
          public string CompanyName { get; set; }
          public string CompanyLogo { get; set; }
          public string TitlePic { get; set; }
          public string CopyText { get; set; }
          public string RegText { get; set; }
      }
  </code></pre>
  
  <p>and </p>
  
  <blockquote>
    <p>Employee model is like</p>
  </blockquote>
  
  <pre><code>[Table("tblEmployee")]
      public class Employee:CompanyProfile
      {
          [Key]
          public int EmpID { get; set; }
          public string Name { get; set; }
          public string Gender { get; set; }
          public int DepartmentId { get; set; }
          public string EmpNo { get; set; }
      }
  </code></pre>
  
  <p>and </p>
  
  <blockquote>
    <p>Department model is like</p>
  </blockquote>
  
  <pre><code>[Table("tblDepartment")]
      public class Department:CompanyProfile
      {
          [Key]
          public int DeptID { get; set; }
          public string DeptName { get; set; }
      }
  </code></pre>
  
  <p>as you can see both the models are inherited from </p>
  
  <blockquote>
    <p>CompanyProfile</p>
  </blockquote>
  
  <p>model. Now my problem is when I am trying to access CompanyProfile model in my controller it is giving me an error </p>
  
  <blockquote>
    <p>"Invalid column name 'CompanyId'.\r\nInvalid column name 'CompanyId'"</p>
  </blockquote>
  
  <p>because entity framework is creating the table on behalf of model and joining these tables and tries to find </p>
  
  <blockquote>
    <p>CompanyId in tblEmployee</p>
  </blockquote>
  
  <p>to the best of my knowledge. </p>
  
  <blockquote>
    <p>SampleContext class is like</p>
  </blockquote>
  
  <pre><code>public class SampleContext:DbContext
      {
          public DbSet&lt;Employee&gt; empList { get; set; }
          public DbSet&lt;Department&gt; deptList { get; set; }
          public DbSet&lt;CompanyProfile&gt; Profile { get; set; }
      }
  </code></pre>
  
  <blockquote>
    <p>and HomeController is like</p>
  </blockquote>
  
  <pre><code>SampleContext context=new SampleContext();
          public ActionResult Index()
          {
              CompanyProfile profile;
              if (Session["Profile"] == null)
              {
                  Session["Profile"] = context.Profile.FirstOrDefault();
              }
              profile = (CompanyProfile)Session["Profile"];
              return View(profile);
          }
  </code></pre>
  
  <blockquote>
    <p>Here is my Layout.cshtml code</p>
  </blockquote>
  
  <pre><code> @model ShareLayoutToContent.Models.CompanyProfile
  &lt;head&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title&gt;@ViewBag.Title&lt;/title&gt;
      &lt;link rel="shortcut icon" href="@Url.Content(Model.TitlePic)"&gt;
  &lt;/head&gt;
  </code></pre>
  
  <p>Now can you please provide where I am committing mistakes. Thanks in advance.</p>