Base solution for your next web application
Open Closed

How to specify sql table name when using IDapperRepository<MyEntity> #7220


User avatar
0
mightyit created

Let's say we have a table called "MyEntities" in SQL Server with the corresponding IEntity class "MyEntity"; The class "MyEntity" is decorated with the [TableName("MyEntities")] attribute.

When using IDapperRepository<MyEntity> to query the database, it unfortunately resolves to the singular form for the table name, in stead of the TableName attribute specified.

Is there any way to correct this behaviour?

[Table("PersonRecords")]
public class PersonRecord : FullAuditedEntity<Guid>, IMayHaveTenant
{
          //More attributes
}
// Meanwhile, in a client class somewhere....
public class Consumer 
{

    private readonly IDapperRepository<PersonRecord, Guid> _personRecordDapperRepository;

    public Consumer(
        IDapperRepository<PersonRecord, Guid> personRecordDapperRepository)
    {
        _personRecordDapperRepository = personRecordDapperRepository;
    }

    public void DoSomething()
    {
            _personRecordDapperRepository.GetAll(); // <== Generates exception
    }
}

Here is the exception:

System.Data.SqlClient.SqlException HResult=0x80131904 Message=Invalid object name 'PersonRecord'. Source=Core .Net SqlClient Data Provider StackTrace: at System.Data.SqlClient.SqlConnection.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.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) at Dapper.SqlMapper.ExecuteReaderWithFlagsFallback(IDbCommand cmd, Boolean wasClosed, CommandBehavior behavior) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1064 at Dapper.SqlMapper.<QueryImpl>d__1381.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:line 1081 at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 723 at DapperExtensions.DapperImplementor.GetList[T](DbConnection connection, IClassMapper classMap, IPredicate predicate, IList1 sort, DbTransaction transaction, Nullable1 commandTimeout, Boolean buffered) at DapperExtensions.DapperImplementor.GetList[T](DbConnection connection, Object predicate, IList1 sort, DbTransaction transaction, Nullable1 commandTimeout, Boolean buffered) at DapperExtensions.DapperExtensions.GetList[T](DbConnection connection, Object predicate, IList1 sort, DbTransaction transaction, Nullable1 commandTimeout, Boolean buffered) at Abp.Dapper.Repositories.DapperRepositoryBase2.GetAll() at Castle.Proxies.Invocations.DapperRepositoryBase2_GetAll.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.DapperEfRepositoryBase3Proxy.GetAll()


4 Answer(s)
  • User Avatar
    0
    mightyit created

    @ismcagdas Any ideas?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @mightyit

    Could you try specifying table name in the OnModelCreating method of your DbContext using fluent configuration ?

  • User Avatar
    0
    mightyit created

    @ismcagdas I have done that. The error persists.

    Guys, I am having serious issues with data access on backgroundjobs, both using EF and Dapper. See also https://support.aspnetzero.com/QA/Questions/7219

    Can we please escalate this to get to a resolution? This is putting my project at serious risk. Please contact me via email to resolve it directly.

    System.Data.SqlClient.SqlException HResult=0x80131904 Message=Invalid object name 'PersonRecord'. Source=Core .Net SqlClient Data Provider StackTrace: at System.Data.SqlClient.SqlConnection.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.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) at Dapper.SqlMapper.ExecuteReaderWithFlagsFallback(IDbCommand cmd, Boolean wasClosed, CommandBehavior behavior) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1064 at Dapper.SqlMapper.<QueryImpl>d__1381.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:line 1081 at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 723 at DapperExtensions.DapperImplementor.GetList[T](DbConnection connection, IClassMapper classMap, IPredicate predicate, IList1 sort, DbTransaction transaction, Nullable1 commandTimeout, Boolean buffered) at DapperExtensions.DapperImplementor.GetList[T](DbConnection connection, Object predicate, IList1 sort, DbTransaction transaction, Nullable1 commandTimeout, Boolean buffered) at DapperExtensions.DapperExtensions.GetList[T](DbConnection connection, Object predicate, IList1 sort, DbTransaction transaction, Nullable1 commandTimeout, Boolean buffered) at Abp.Dapper.Repositories.DapperRepositoryBase2.GetAll() at Castle.Proxies.Invocations.DapperRepositoryBase2_GetAll.InvokeMethodOnTarget() at Castle.DynamicProxy.AbstractInvocation.Proceed() at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.DapperEfRepositoryBase3Proxy.GetAll() at MyModule.SearchServices.Infrastructure.UnscConsolidatedSanctionList.Infrastructure.Persons.PersonMonitoringBatch.PersonMonitoringBatchSearchJob.ExecuteJob(PersonMonitoringBatchSearhJobArgs args) in D:\Projects\MySystem\Modules\MyModule\MyModule.SearchServices.Infrastructure.UnscConsolidatedSanctionList\Infrastructure\Persons\PersonMonitoringBatch\PersonMonitoringBatchSearchJob.cs:line 91 at MyModule.SearchServices.Infrastructure.UnscConsolidatedSanctionList.Core.BatchSearchJobBase`1.Execute(TBatchSearhJobArgs args) in D:\Projects\MySystem\Modules\MyModule\MyModule.SearchServices.Infrastructure.UnscConsolidatedSanctionList\Core\BatchSearchJobBase.cs:line 20

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @mightyit

    Could you share your project with [email protected] ?

    Thanks,