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).
So I have the following code for one of Controllers:
[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 => p.UserID.Equals((int) id));
db.LoadProperty(user, p => p.UserProfiles);
db.LoadProperty(user, p => 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 => p.CompanyID == 8)
};
db.Users.InsertOnSaveChanges(user);
}
TryUpdateModel(user);
db.SaveChanges();
}
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.
Entities in 'UMSEntities.UserCompanyProfiles' participate in the 'FK__UserCompa__Compa__25869641' relationship. 0 related 'Companies' were found. 1 'Companies' is expected.
By the way, it does in fact return one Company object (it is of type PocoProxies.CompanyProxy). Any help would certainly be appreciated!