title: Entity Framework - Code First relationship: one-to-one
body: <p>I have two tables Users and Companies:</p>
  
  <pre><code>public class User
  {
      // Properties
      public long Id { get; set; }
  
      public string FirstName { get; set; }
  
      public string LastName { get; set; }
  
      public string Username { get; set; }
  
      public long AgencyId { get; set; }
      public Company Company { get; set; }
  
      // Custom Propreties
      [ScaffoldColumn(false)]
      public string FullName
      {
          get
          {
              return FirstName + " " + LastName;
          }
      }
  }
  
  public class Company
  {
      public long Id { get; set; }
  
      public string Name { get; set; }
  
      public virtual ICollection&lt;User&gt; Users { get; set; }
  }
  </code></pre>
  
  <p>The configuration is as so...</p>
  
  <pre><code>public class UserConfiguration : EntityTypeConfiguration&lt;User&gt;
  {
      public UserConfiguration()
      {
          this.HasKey(x =&gt; x.Id);
  
          this.Property(x =&gt; x.Id);
          this.Property(x =&gt; x.FirstName).IsRequired();
          this.Property(x =&gt; x.LastName).IsRequired();
          this.Property(x =&gt; x.Username).IsRequired();
          this.Property(x =&gt; x.CompanyId).IsRequired();
  
          this.HasRequired(user =&gt; user.Company).WithMany().HasForeignKey(user =&gt; user.CompanyId);
      }
  }
  
  public class CompanyConfiguration : EntityTypeConfiguration&lt;Company&gt;
  {
      public CompanyConfiguration()
      {
          this.ToTable("Companies");
  
          this.HasKey(x =&gt; x.Id);
  
          this.Property(x =&gt; x.Id);
          this.Property(x =&gt; x.Name).IsRequired();
  
          this.HasMany(company =&gt; company.Users).WithRequired().HasForeignKey(user =&gt; user.CompanyId);
      }
  }
  </code></pre>
  
  <p>If I create a view with the Companies to show each company and make one column the Count of Users in the Company, then the View is rendered as expected showing the number of Users in each company.  However, when I try to show each user in a View and show there Company.Name in a column, then it says that Company is null.  Can someone explain if my one-to-one relationship is screwed up between User and Company?</p>
  
  <p><em><strong></em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em></strong> EDIT <strong><em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>***</strong></p>
  
  <pre><code>    public UserConfiguration()
      {
          this.HasKey(x =&gt; x.Id);
  
          this.Property(x =&gt; x.Id);
          this.Property(x =&gt; x.FirstName).IsRequired();
          this.Property(x =&gt; x.LastName).IsRequired();
          this.Property(x =&gt; x.Username).IsRequired();
          this.Property(x =&gt; x.CompanyId).IsRequired();
  
          this.HasRequired(user =&gt; user.Company).WithMany().HasForeignKey(user =&gt; user.CompanyId);
          this.HasMany(user =&gt; user.AdministratorApplications)
              .WithMany(application =&gt; application.Administrators)
              .Map(map =&gt;
              {
                  map.ToTable("ApplicationAdministrators");
                  map.MapLeftKey("ApplicationId");
                  map.MapRightKey("UserId");
              });
      }
  
      public ApplicationConfiguration()
      {
          this.HasKey(x =&gt; x.Id);
  
          this.Property(x =&gt; x.Name).IsRequired();
          this.Property(x =&gt; x.Accronym).IsRequired();
          this.Property(x =&gt; x.Description);
  
          this.HasMany(application =&gt; application.Administrators)
              .WithMany(user =&gt; user.AdministratorApplications)
              .Map(map =&gt;
              {
                  map.ToTable("ApplicationAdministrators");
                  map.MapLeftKey("UserId");
                  map.MapRightKey("ApplicationId");
              });
      }
  
      public ApplicationAdministratorConfiguration()
      {
          this.ToTable("ApplicationAdministrators");
  
          this.HasKey(x =&gt; x.Id);
  
          this.Property(x =&gt; x.Id);
          this.Property(x =&gt; x.ApplicationId).IsRequired();
          this.Property(x =&gt; x.UserId).IsRequired();
  
          this.HasRequired(appAdmin =&gt; appAdmin.Application).WithMany().HasForeignKey(appAdmin =&gt; appAdmin.ApplicationId);
          this.HasRequired(appAdmin =&gt; appAdmin.User).WithMany().HasForeignKey(appAdmin =&gt; appAdmin.UserId);
      }
  </code></pre>
  
  <p>Here is the ApplicationAdministrator class</p>
  
  <pre><code>public class ApplicationAdministrator
  {
      [Column("Id")]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      [HiddenInput]
      public long Id { get; set; }
  
      [Display(Name = "Application")]
      public long ApplicationId { get; set; }
      public virtual Application Application { get; set; }
  
      [Display(Name = "Administrator")]
      public long UserId { get; set; }
      public virtual User User { get; set; }
  }
  </code></pre>
  
  <p>And finally the error</p>
  
  <blockquote>
    <p>Schema specified is not valid. Errors:  (144,6) : error 0019: The
    EntitySet 'UserApplication' with schema 'dbo' and table
    'ApplicationAdministrators' was already defined. Each EntitySet must
    refer to a unique schema and table.</p>
    
    <p>Line 15:         public IQueryable Users 
    Line 16:         { 
    Line 17:             get { return context.Users.Include("AdministratorApplications").Include("Company"); } 
    Line 18:         } 
    Line 19:</p>
  </blockquote>