I have a class called Business, which has a property Id that is a identity field in MS SQL Server database. When I do an INSERT, it works properly. If I do the following:
_db.Update(business);
I get an error:
invalid column name 'id" the column in the DB is BusinessId
I have mapped it to Id as below:
public class BusinessMap : EntityMap<Business>
{
public BusinessMap()
{
Map(x => x.Id).ToColumn("BusinessId");
}
}
Now keep in mind my INSERT works. I tried adding the Key attribute to the class
[Table("Business")]
public class Business {
[Key]
public int Id { get; set; }
public string Name { get; set; }
.....
}
My work around (which I hate, but is better than nothing):
I decided to just call a SP to do this, which sort of sucks because I was hoping dapper could handle this.
_db.Query("BusinessUpdate",
param: new { businessId = business.Id, business.Name, business.LocatorUrl, business.Website }, commandType: CommandType.StoredProcedure);
I don't know why we are forced to conclude that every identity column in our database is named "ID", that is ludicrous in my opinion, and I don't think I need to justify my schema design, it should let me change it. Now anytime a field is added, I have to change the SP and the code to call the SP, I will use this until I found the real answer.
You are using Dapper.FluentMap. This tool works as mapper for Dapper; NOT for Dapper Contrib.
So while generating the query, Contrib do not know
BusinessIdcolumn is mapped withIdproperty. It assumes that forIdproperty, there exist anIdcolumn. TheINSERTis working because your primary key is automatically being generated by RDBMS you are using.To know more about mapping column/table names to properties/classes, please refer to this question.
If you are trying to update/change the ID using Dapper Contrib, then it is not supported. This is because ORMs like Dapper Contrib use ID for uniquely identifying the record while generating queries.
So when you call
_db.Update(business);method, Dapper Contrib generate the query something likeUPDATE <table name> SET <column list except ID, with new values> WHERE <ID column> = ?. As you can see, theIDis being used to uniquely identify the record to be updated while generatingUPDATEquery.Unfortunately, I could not find reference for this. The GitHub help also does not explicitly mention it.
The alternative is to bypass the Dapper Contrib and use Dapper's
Executemethod directly by writing query by hand. I am not sure but Dapper.FluentMap also have CRUD query generator named Dommel; you may try one-to-one mapping that it support.Although you are using Dapper Contrib, most part of this answer is also applicable to Dapper Extensions or some other query generators associated with Dapper.