views:

40

answers:

1

I'm trying to add an object created from Entity Data Model classes. I have a table called Users, which has turned into a User EDM class. And I also have a table Pages, which has become a Page EDM class. These tables have a foreign key relationship, so that each page is associated with many users. Now I want to be able to add a page, but I can't get how to do it. I get a nullreference exception on Users below. I'm still rather confused by all this, so I'm sure it's a simple error, but I just can't see how to do it.

Also, by the way, the compiler requires that I set PageID in the object initializer, even though this field is set to be an automatic id in the table. Am I doing it right just setting it to 0, expecting it to be updated automatically in the table when saved, or how should I do that?

Any help appreciated!

The method in question:

private Page GetPage(User currentUser)
{
   string url = _request.ServerVariables["url"].ToLower();
   var userPages = from p in _context.PageSet
                     where p.Users.UserID == currentUser.UserID
                     select p;
   var existingPage = userPages.FirstOrDefault(e => e.Url == url);

   if (existingPage != null)
      return existingPage;

   Page page = new Page()
                   {
                      Count = 0,
                      Url = _request.ServerVariables["url"].ToLower(),
                      PageID = 0, //Only initial value, changed later?
                   };

   _context.AddToPageSet(page);
   page.Users.UserID = currentUser.UserID; //Here's the problem...
   return page;
}
+1  A: 

Try:

page.Users = _context.Users.First(u => u.UserId == currentUser.UserID);

Regarding this:

Also, by the way, the compiler requires that I set PageID in the object initializer

That should not be true. What is the exact error message? Also, what is the type of the ID?

Craig Stuntz
Thanks, but unfortunately it doesn't work... First, your expression tries to assign a User object to an int (UserID). So I changed to page.Users.UserID = _context.UserSet.First(u => u.UserID == currentUser.UserID).UserID; But that doesn't work either. Still nullreference on the Users part of page.Users.UserID. I'm trying to add a new page row, and then set the foreign key UserID on that new page to the current User objects UserID (primary key). Am I missing something here? Obviously the Users collection of the new page is empty, but what do I need to do to add it then?
Anders Svensson
Sorry; copy/paste error. Fixed. Based on your code above, `Users` looks like a single `User`, not a collection.
Craig Stuntz
You're right, it's not a collection. But why would it be called Users then...? But this works! I investigated a little further though, and e.g. here it's recommended not to do it this way: http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/ (due to overhead apparently). But the alternative code is really over-complicated, so I'd rather go with your suggestion. Any take on this? (I'm using EF 3.5 btw, is 4.0 different in this respect?). Thanks for your help!
Anders Svensson
About the Users property, apparently it is a "Navigation property", looking in the Entity model designer. They're a bit confusing... Besides my question above, my User EDM class also got 3 navigation properties - "Pages", "Users1", and "Users2", while the Page EDM class only got the one "Users" navigation property. What is going on with these?
Anders Svensson
It's called `Users` because EF 1 doesn't automatically (de)pluralize. EF 4 does. Regarding `Users1`, etc, look at mapping details in the EF desinger; it should tell you what they represent.
Craig Stuntz
Ok great, thanks!
Anders Svensson