Is it necessary to use TransactionScope when using entity framework? Following article suggest TransactionScope is not needed at all.
https://coderwall.com/p/jnniww/why-you-shouldn-t-use-entity-framework-with-transactions
In my current application I'm using Transactions in the following manner which causes high CPU in database server. Can I get rid of TransactionScope? and still expect the records to save to the database only if all the inserts suceeded when there are multiple tables involved?
public string AddNewCustomRole(CustomRoleViewModel customRole)
{
using (TransactionScope scope = new TransactionScope())
{
CustomRole role = new CustomRole()
{
CreatedOnUtc = DateTime.UtcNow,
CustomRoleName = customRole.CustomRoleName,
LastModifiedUtc = DateTime.UtcNow,
CompanyId = customRole.CompanyId,
CreatedBy = customRole.UserId,
LastModifiedBy = customRole.UserId,
IsAdmin = false,
Ref = Guid.NewGuid()
};
_context.CustomRole.Add(role);
var result = _context.CustomRolePermission;
foreach (var item in result)
{
CustomRoleRolepermission permission = new CustomRoleRolepermission()
{
RolePermissionId = item.RolePermissionId,
CustomRoleId = role.CustomRoleId
};
_context.CustomRoleRolepermission.Add(permission);
}
_context.SaveChanges();
scope.Complete();
return role.Ref.ToString();
}
}