DapperLib / Dapper.Contrib

Dapper community contributions - additional extensions for Dapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dapper.Contrib INSERT (list) needs the table name to be Plural with an "s" at the end

alexvilper opened this issue · comments

Hello guys!

Thanks for this project, it's helping me a lot.
I found what I think to be a bug, please correct me if I am wrong.

Using the example for "Insert Many" from this link :

`using (var connection = My.ConnectionFactory())
{
connection.Open();

var list = new List<InvoiceContrib>
{
    new InvoiceContrib {Kind = InvoiceKind.WebInvoice, Code = "Insert_Many_1"},
    new InvoiceContrib {Kind = InvoiceKind.WebInvoice, Code = "Insert_Many_2"},
    new InvoiceContrib {Kind = InvoiceKind.StoreInvoice, Code = "Insert_Many_3"}
};

var identity = connection.Insert(list);

}`

I noticed that, the table name in this case will need to be InvoiceContribs - otherwise an error as below will be thrown:

System.Data.SqlClient.SqlException
HResult=0x80131904
Message=Invalid object name 'Pings'.
Source=Core .Net SqlClient Data Provider
StackTrace:

For instance. My POCO is called "Ping" - and my table name was "Ping" - but the error said Invalid object name 'Pings'. As soon as I renamed the table to Pings it worked.

Call Stack:

StackTrace:
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.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.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in //Dapper/SqlMapper.cs:line 554
at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable1 commandType) in /
/Dapper/SqlMapper.cs:line 443
at Dapper.Contrib.Extensions.SqlMapperExtensions.Insert[T](IDbConnection connection, T entityToInsert, IDbTransaction transaction, Nullable`1 commandTimeout) in /_/Dapper.Contrib/SqlMapperExtensions.cs:line 384

Can anyone please confirm this issue?

Thanks!
Alex

Hi, I resolved the issue by adding the tablename attribute in my POCO.

This is not a bug. The README states:

Special Attributes
Dapper.Contrib makes use of some optional attributes:

  • [Table("Tablename")] - use another table name instead of the (by default pluralized) name of the class
[Table ("emps")]
public class Employee
{
   public int Id { get; set; }
   public string Name { get; set; }
}

Meaning that if you don't specify the TableAttribute like in the example, Dapper will expect the pluralized name of the class to be the table name, e.g.: "Employees"

This issue can be closed.