Search code examples
c#insertlinq2db

linq2db Insert ? error ColumnMapping


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.


Solution

  • 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.