question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need to get AbpUsers table data by conventional DbContext way.

See original GitHub issue

Hi dear @ismcagdas , @maliming , @acjh , @hikalkan , @alirizaadiyahsi I want to get the data from AbpUsers table in conventional way without using any repositories, UserManager Services etc.

Because, as I am converting my normal MVC source to ABP (#4234) it is impossible to change all the existing code to ABP standards. So my plan is to run the existing code as it is and follow ABP style for further development. I am very happy that it is almost successful and running.

Currently I am facing an important issue regarding the eager loading of AbpUsers table. Not only for eager loading, but also even i can’t get results of querying the AbpUsers table directly.

My Linq query in Controller is given below. The controller is derived from ABMControllerBase only (ABM is the project name):

 private ABMDbContext db = new ABMDbContext();

 var result = db.POMasters
                      .Include(x => x.PORevisionRemarks.Select(c => c.UserMaster))
                      .Include(x => x.UserMaster)
                      .Where(x => !x.IsDeleted && x.IsSubmitted)
                      .MapTo<List<POMasterListDto>>();

 // Trying to get the Users list by conventional way just for testing. 
 // But unfortunately, this also returns null
 var userList = db.Users.ToList();

Without including the UserMaster and PORevisionRemarks.UserMaster, the query runs smoothly. Also I can get results when including only PORevisionRemarks too.

When using the given query, I am getting the following error:

  The transaction has aborted.
System.Transactions.TransactionAbortedException: The transaction has aborted. ---> 
System.Data.SqlClient.SqlException: The transaction operation cannot be performed because there are pending requests working on this transaction.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlDelegatedTransaction.SinglePhaseCommit(SinglePhaseEnlistment enlistment)
   --- End of inner exception stack trace ---
   at System.Transactions.TransactionStateAborted.EndCommit(InternalTransaction tx)
   at System.Transactions.CommittableTransaction.Commit()
   at System.Transactions.TransactionScope.InternalDispose()
   at System.Transactions.TransactionScope.Dispose()
   at Abp.EntityFramework.Uow.TransactionScopeEfTransactionStrategy.Commit()
   at Abp.EntityFramework.Uow.EfUnitOfWork.CompleteUow()
   at Abp.Domain.Uow.UnitOfWorkBase.Complete()
   at Abp.Web.Mvc.Uow.AbpMvcUowFilter.OnActionExecuted(ActionExecutedContext filterContext)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_1.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_1.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_1.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_1.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_6.<BeginInvokeAction>b__3()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass3_1.<BeginInvokeAction>b__5(IAsyncResult asyncResult)

The PORevisionRemark class:

public partial class PORevisionRemark
{
	public long Id { get; set; }

	public long POMasterId { get; set; }

	public string PONo { get; set; }

	public int RevisionNo { get; set; }

	public string Remarks { get; set; }

	public long RevisedBy { get; set; }
	[ForeignKey("RevisedBy")]
	public virtual User UserMaster { get; set; }

	public System.DateTime RevisedOn { get; set; }
    
        public virtual POMaster POMaster { get; set; }        
}

Also I have given a custom mapping between POMaster and POMasterListDto as follows:

cfg.CreateMap<POMaster, POMasterListDto>()
     .ForMember(dt => dt.CreatedUserName, op => op.MapFrom(s => s.UserMaster.Name ?? "User not Found"))
     .ForMember(dt => dt.RevisedUserName, op => op.MapFrom(s => s.PORevisionRemarks.Where(z => z.RevisionNo == s.CurrentRevisionNo).FirstOrDefault() == null ? "" : s.PORevisionRemarks.Where(z => z.RevisionNo == s.CurrentRevisionNo).FirstOrDefault().UserMaster.Name))

The same code is running successfully in my old project with another table instead of AbpUsers. Please help me out.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
malimingcommented, Mar 29, 2019

@sayassyk Try not to create objects manually, using dependency injection. https://github.com/sayassyk/module-zero-template/blob/master/src/AbpCompanyName.AbpProjectName.WebMpa/Controllers/UsersController.cs#L36

private readonly IUserAppService _userAppService;
private readonly RoleManager _roleManager;
private readonly AbpProjectNameDbContext db;

public UsersController(IUserAppService userAppService, RoleManager roleManager, AbpProjectNameDbContext dbContext)
{
	_userAppService = userAppService;
	_roleManager = roleManager;
	db = dbContext;
}
0reactions
sayassykcommented, Mar 31, 2019

Made my day!! It worked!!! Thank you so much @maliming , @ismcagdas , @ryancyq for your awesome support!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't use my joining table from DbContext
ProjectUser - is joining table, use for link two tables. This tables must have (usually) Collection for access to this many-to-many data. So...
Read more >
How to split Tenant and User Data #11281 | Support Center
Each tenant type will need different tables so I am thinking I need 3 db context, 1 for the Main ABP stuff and...
Read more >
DbContext Lifetime, Configuration, and Initialization
This article shows basic patterns for initialization and configuration of a DbContext instance. The DbContext lifetime. The lifetime of a ...
Read more >
Working with DbContext - EF6
NET objects, you first need to Create a Model which maps the entities and relationships that are defined in your model to tables...
Read more >
EF Core Database Migrations - ABP Documentation
DbMigrations merges all the database mappings of all the modules (plus your application's mappings) to create a unified migration path. An ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found