I have 2 connections. How can I reduce this to one connection?
//AuthentificationController class:
public string Register(string nickName, string email, string password)
{
try
{
if(!UserWorker.IsUserRegistered(nickName)) //connect
{
UserWorker.RegisterUser(nickName, email, password)) //connect
return "Done";
}
else
{
return "You are already registered";
}
}
catch(Exception ex)
{
//log
return "Server error";
}
}
//UserWorker class:
//...
bool IsUserRegistered(string nickName)
{
using(var context = new XContext)
{
return context.Users.Contains(x => x.NickName == nickName);
}
}
//...
void RegisterUser(string nickName, string email, string password)
{
using(var context = new XContext)
{
User newUser = new User(nickName, email, password);
context.Users.Add(newUser);
context.SaveChanges();
}
}
var context = new XContext
isn't valid C#). That said, you're using two contexts, that doesn't mean you're using two connections - you may want to read up on connection pooling and how EF is handling that. – Mat's Mug♦ Jan 30 at 17:07