Reputation: 113
I try use linq2db for insert data? but get error
Table class
[Table(Schema="inf", Name="InformMessageLog")]
public partial class InformMessageLog
{
[Identity ] public int ID { get; set; }// int
[Column, NotNull ] public DateTime Date { get; set; } // datetime
[Column, NotNull ] public int StudentID { get; set; }// int
[Column, Nullable] public string ContactName { get; set; } // nvarchar(max)
[Column, Nullable] public string ContactPhone { get; set; } // nvarchar(max)
[Column, Nullable] public string ContactMail { get; set; }// nvarchar(max)
[Column, NotNull ] public string EventPoint { get; set; } // nvarchar(50)
[Column, NotNull ] public string Template { get; set; } // nvarchar(max)
[Column, NotNull ] public string Link { get; set; } // nvarchar(100)
[Column, NotNull ] public string Status { get; set; } // nvarchar(100)
[Column, Nullable] public bool? TechnicalError { get; set; } // bit
[Column, Nullable] public string CampaingId { get; set; } // nvarchar(max)
}
my code
List<InformMessageLog> result = new List<InformMessageLog>();
result = ....; //form list result
try
{
using (var db = new IntegrationSqlDbDB())
{
db.BulkCopy(result);
}
return req.CreateResponse(HttpStatusCode.OK, result);
}
catch (Exception e)
{
loger.LogError("Failed to set log from db " + e.Message);
return req.CreateResponse(HttpStatusCode.BadRequest, "Failed to set log from db - " + e.Message);
}
result Data
[
{
"ID": 1,
"Date": "2018-04-13T00:00:00+00:00",
"StudentID": 76769,
"ContactName": "XXXXXXX XXXXXXX",
"ContactPhone": "-",
"ContactMail": "[email protected]",
"EventPoint": "loyality",
"Template": "1806123",
"Link": "unisender",
"Status": "-",
"TechnicalError": false,
"CampaingId": "1594676730"
}
]
error
The given ColumnMapping does not match up with any column in the source or destination.
I checked all types and made a copypaste of the column names, but the problem remained. Could there be an issue in the ID column? How to correctly pass it to the bulkCopy structure.
Upvotes: 0
Views: 882
Reputation:
While SQL Server column names are case-insensitive by default, bulk copy is case-sensitive. You need to check column names in database and if they differ by case from C# properties - put db names into column attribute.
Upvotes: 0
Reputation: 248
To try to pass the ID column, you can try the BulkCopyOptions:
db.BulkCopy(new BulkCopyOptions { KeepIdentity = true }, result);
So, in this case the insertion of all the list will keep the identity that you gave before.
Also, instead of "bit" in column type, try to use boolean.
PS: This option is working in SQL Provider. If you are using PostgreSQL, you need to update the linq2db to version 2.0.0.0 beta4, or some pre-releases.
Upvotes: 1