In the following code GetUserAction does return an instance of action but when the user instance is submitted to the database it creates an additional action row in the database instead of creating a relationship with the existing row that is returned? Why?
using (UserRepository repository = new UserRepository())
{
var user = new user
{
user_created = DateTime.Now,
user_email = email,
user_password = GetHashedPassword(password)
};
// create an entry in the users history
user.user_histories.Add(new user_history
{
user_history_date = DateTime.Now,
action = GetUserAction("Registered")
});
// commit the new user to the database
repository.InsertUser(user);
repository.Save();
}
public static action GetUserAction(string userAction)
{
action a = null;
using (UserRepository repository = new UserRepository())
{
a = repository.SelectUserAction(userAction);
// in the SO example I know a is not null so ignore the next 8 lines
if (a == null)
{
a = new action
{
action_name = userAction
};
}
}
return a;
}