I think i am doing something wrong in here so i will paste here so you can put me to the right track:
public MembershipCreateStatus CreateUser(string username, string password, string mail, bool autoemail, string fullname)
{
using (TemplateEntities ctx = new TemplateEntities())
{
using (TransactionScope tran = new TransactionScope())
{
if (GetUser(username)!=null)
return MembershipCreateStatus.DuplicateUserName;
if (requiresUniqueEmail && !String.IsNullOrEmpty(GetUserNameByEmail(email)))
return MembershipCreateStatus.DuplicateEmail;
User userToCreate = new User
{
UserName=username,
PassWord=EncodePassword(password),
FullName=fullname,
Email=email,
AutoEmail=autoemail
};
try
{
ctx.Users.AddObject(userToCreate);
ctx.SaveChanges();
tran.Complete();
return MembershipCreateStatus.Success;
}
catch
{
return MembershipCreateStatus.UserRejected;
}
}
}
}
public override string GetUserNameByEmail(string email)
{
using (TemplateEntities ctx = new TemplateEntities())
{
return (from u in ctx.Users
where u.Email == email
select u.UserName).SingleOrDefault();
}
}
public User GetUser(string username)
{
using (TemplateEntities ctx = new TemplateEntities())
{
return (from u in ctx.Users
where u.UserName == username
select u).FirstOrDefault();
}
}
- 1Q)I am opening 3 connections to the database. Is it ok? or do i have to have only one connection and how to manage that correctly?
- 2Q)Do i need transaction here and how to manage that correctly?