title: Entity Framework Foreign Key (or lack thereof)
body: <p>So I have pouring of this code forever, trying to figure this out...  I am using Entity Framework 1.0 with ASP.NET MVC in .NET 3.5 SP1 with EFPocoAdapter (so separate Poco classes).</p>
  
  <p>So I have the following code for one of Controllers:</p>
  
  <pre><code>    [AcceptVerbs(HttpVerbs.Post)]
      public ActionResult EditUser(int? id, FormCollection form)
      {
          var user = new User();
  
          using (var db = new UMSEntities { DeferredLoadingEnabled = false })
          {
              if (id != null)
              {
                  user = db.Users.FirstOrDefault(p =&gt; p.UserID.Equals((int) id));
                  db.LoadProperty(user, p =&gt; p.UserProfiles);
                  db.LoadProperty(user, p =&gt; p.UserCompanyProfile);
              }
              else
              {
                  user.UserGuid = Guid.NewGuid();
                  user.DateCreated = DateTime.Now;
                  user.DateLastActivity = DateTime.Now;
                  user.DateModified = DateTime.Now;
                  user.Password = "demo";
                  user.Version = 0;
                  user.SecretAnswer = "";
                  user.SecretQuestion = "";
                  user.IsApproved = true;
                  user.IsActive = true;
  
                  user.UserProfiles = new UserProfile();
                  user.UserCompanyProfile = new UserCompanyProfile
                        {
                            EmployeeID = "",
                            HireDate = DateTime.Now,
                            Title = "",
                            UserCode = "",
                            Company = db.Companies.First(p =&gt; p.CompanyID == 8)
                        };
  
                  db.Users.InsertOnSaveChanges(user);
  
              }
  
              TryUpdateModel(user);
  
              db.SaveChanges();
          }
  </code></pre>
  
  <p>Editing an exist user works perfect (notice if id != null).  However, it is the portion of code that adds a new user.  It works until I get to the portion of code where I add the UserCompanyProfile to the user object.  If I comment this code out, it works just fine.  The problem arises when I try to attach a company to the UserCompanyProfile by querying the database-- I get this error.</p>
  
  <pre><code>Entities in 'UMSEntities.UserCompanyProfiles' participate in the 'FK__UserCompa__Compa__25869641' relationship. 0 related 'Companies' were found. 1 'Companies' is expected.
  </code></pre>
  
  <p>By the way, it does in fact return one Company object (it is of type PocoProxies.CompanyProxy).  Any help would certainly be appreciated!</p>