I want to Save User Information In the Database.
public async Task<User> CreateAsync(CreateRequestViewModel user)
{
User NewUser = new User
{
UserId = Guid.NewGuid(),
Password = user.Password,
UserName = user.UserName,
};
var resultUser = await _ProjectContext.Users.AddAsync(NewUser);
Person person = new Person()
{
UserId = resultUser.Entity.UserId,
RegisterDate = DateTime.Now,
Email = user.Email,
Gender = user.Gender,
PersonId = Guid.NewGuid(),
};
await _ProjectContext.Persons.AddAsync(person);
return resultUser.Entity;
}
User Service Code:
public async Task<User> Create(CreateRequestViewModel user)
{
try
{
var model = await _unitOfWork.Users.CreateAsync(user);
await _unitOfWork.CommitAsync();
return model;
}
catch (Exception me)
{
throw;
}
}
but when to run this code:
await _unitOfWork.CommitAsync();
code in CommitAsync:
public async Task<int> CommitAsync()
{
int oo = 0;
try
{
oo = await _context.SaveChangesAsync();
}
catch (Exception me)
{
throw;
}
return oo;
}
I get this error:
Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ProjectContext'.
How it can be fixed.