I have a .Net Core 3.1 Console application.
In SQL Server database I have tables with singular names, the same as my POCO classes, which is convenient for matching and maintaining.
For Insert, Update and Delete operations I want to use Dapper.Contrib
library. But when I run Insert function Dapper pluralize table names in generated SQL queries.
SQL table "Student":
CREATE TABLE Student
StudentId int NOT NULL PRIMARY KEY,
FirstName varchar(50),
LastName varchar(50)
C# code
public class Student
{
public int StudentId {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
class Program
{
static void Main(string[] args)
{
var students = new List<Student>()
{
new Student { StudentId = 1, FirstName = "John", LastName = "Doe"},
new Student { StudentId = 2, FirstName = "Tony", LastName = "Montana"}
}
long result;
using (var cn = new SqlConnection(connString))
{
result = cn.Insert(students);
}
}
}
On output I get an exception:
Invalid object name 'Students'.
I surfed around to find a solution, but couldn't find a working example. In general there two kinds of recommendations:
Use data annotations. This one is not suitable for me, as there are a lot of POCO objects created automatically from a database using
Scaffold-DbContext
. During the development process, I make changes in database and then re-create POCOs again. This operation deletes all changes made in generated POCO classes.Use of
SqlMapperExtensions
delegate:SqlMapperExtensions.TableNameMapper = (type) => { // do something here to pluralize the name of the type return type.Name; };
I don't know how to properly use this delegate, and where to place it. I experimented a while, but I am sure that I don't use it properly:
using (var cn = new SqlConnection(connString))
{
SqlMapperExtensions.TableNameMapper = (type) =>
{
type.Name.Remove(type.Name.Length - 1, 1);
return type.Name;
};
result = cn.Insert(students);
}
On this line of code type.Name.Remove(type.Name.Length - 1, 1);
I tried to implement a function which cuts-off the last letter in the name of the type Student
, assuming that Dapper adds the last letter "s" to the name of the class, and I remove it in my implementation.
Example: Student => Students => Student(s) => Student
.
Long story short - I couldn't find a solution of how to implement a function that will "singularize" my table names.
How should I use SqlMapperExtensions
or maybe there is another approach which may "singularize" table names without making changes in POCO classes?