views:

1573

answers:

2

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!

A: 

Does a company with an ID of 8 exist? I would say change your code to get the correct company first and I bet the issue will go away.

There may be something wrong with your Lamda expression, but I am not good enough with them to know.

JasonRShaver
I did mention in my post (at the end) that it returns a Company object, thus meaning the company does indeed exist. I'm pretty sure the Lambda is correct, but please if any one has insight that would be great.
+1  A: 

So I figured it out, oh my word did it take me a long time... so obvious too. Instead of querying Company and associating it through UserCompanyProfile, I needed to associate it the other way around:

                // ...
                user.UserProfiles = new UserProfile();
                user.UserCompanyProfile = new UserCompanyProfile
                      {
                          EmployeeID = "",
                          HireDate = DateTime.Now,
                          Title = "",
                          UserCode = ""
                      };

                var company = db.Companies.First(p => p.CompanyID.Equals(16));
                company.UserCompanyProfile.Add(user.UserCompanyProfile);

                db.Users.InsertOnSaveChanges(user);
                // ...

Hopefully this helps someone else!