다음 코드에서 GetUserAction은 작업 인스턴스를 반환하지만 사용자 인스턴스가 데이터베이스에 제출 될 때 반환되는 기존 행과의 관계를 만드는 대신 데이터베이스에 추가 작업 행을 만듭니다. 왜?Linq하려면 Sql 왜 내가 중복 레코드를 얻고 있습니까?
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;
}
사용자를 저장하는 동일한 컨텍스트에서 사용자 작업을 검색하면 어떻게됩니까? – Gregoire
@Gregoire 나는 그것을 시도하고 괜찮아요, 괜찮아요,하지만 나는 반환 된 작업이 새 것인지 기존인지 알 수있는 경우 @Daniel CS 대답이 작동하는 별도의 메서드로 UserAction 검색을 추출하고 싶습니다. –