A solution explained in this article, is telling EF about the single Database connection. This is done simply by opening the connection yourself after creating the context.
I will try that first since it is involving less work.
using (var ctx = new MyEntities())
{
((IObjectContextAdapter)ctx).ObjectContext.Connection.Open();
....
...all my neat stuff
}
<a class="postlink" href="http://fabzter.com/blog/avoid-distributed-transactions-using-entity-framework">http://fabzter.com/blog/avoid-distribut ... -framework</a>
After some tests, NLog connection does NOT seems to be the culprit.
My second supposition would be the usage of TransactionScope made by ABP. Is there any plan to replace the TransactionScope usage with the new EF6 BeginTransaction ? <a class="postlink" href="https://msdn.microsoft.com/en-us/data/dn456843.aspx">https://msdn.microsoft.com/en-us/data/dn456843.aspx</a> Microsoft now recommend BeginTransaction instead of TransactionScope. Moreover, it is mentioned that TransactionScope cannot be used in cloud scenarios unless you are sure you have one and only one connection. My assumption, I may be wrong, is that no matter how hard we try to use a single connection, we have no control on when the DBContext decide to close and open a new connection, which would cause an escalation to DTC because of the TransactionScope usage. What I suspect that cause this is when SaveChanges is called multiple times in the same ApplicationService method ex: calling Repository.InsertAndGetId() and the SaveChanges() called and the end of UoW.
I will do some tests by replacing TransactionScope with EF6 BeginTransaction to see if it helps.
Thanks for the insight.
For now, I did optimize using this 'hacky' approach. I inject an IRepository<RolePermissionSetting, long>, and get the list of roles which have the permission. Total resulting queries = 2. ** I'm using Permissions only at role level. No user specific permissions.
// get list of roleid that have the permission
var roleIds = _rolePermissionSettingRepository.GetAll()
.Where(x => x.Name == PermissionNames.CanBeAssigneeOnTask)
.Select(x => x.RoleId)
.ToList();
var query = _userRepository.GetAll()
.Where(x => x.IsActive && !x.IsDeleted)
.Where(x => x.Roles.Any(y => roleIds.Contains(y.RoleId)));