by Mikael Henriksson
21. May 2009 23:09
Well first of all model first of course. Second and even more importantly is that I don’t need to think too much about related entities I can just go ahead, add stuff to my entities and then just save it all. Take the following for example.
It’s probably a silly example but I’ll go ahead anyway. Each user can have a profile with some personal details that I don’t want to load unless explicitly told to (you know performance etc). Earlier I had to first create a user, then create a profile have do: user.AddToProfile(profile)
Nothing wrong with that really but it wasn’t optimal. In the new release I can go ahead and do what I really wanted to do:
// Create a user
User user = new User();
user.Alias = "MyCoolAlias";
user.Email = "bogusemail@domain.se";
user.Password = "1234";
user.Salt = "1234";
user.Active = true;
// Create a profile for the user
user.Profile = new Profile();
user.Profile.FirstName = "I am";
user.Profile.LastName = "Impressed";
user.Profile.Address.StreetOne = "By the new changes street in 2.0";
user.Profile.Address.City = "Oslo";
user.Profile.Address.Zip = "0473";
user.Profile.Address.Country = "Norway";
// Add the user to the context and save it to db
Entities context = new Entities();
context.AddToUsers(user);
context.SaveChanges();
How much easier is that I ask you?