1

I am using linq2db .NET Core library to bulk insert a collection. This code can be executed without error but there is no data in the database. There are 2000 person object in persons list object. Person object has already identity in it.

using (var db = SqlServerTools.CreateDataConnection(connstring)
{
    await db.BulkCopyAsync(new BulkCopyOptions { KeepIdentity = true, TableName = "[Persons].[Person]" }, persons);
}

Person table in in Persons schema.

I have also tried with BulkCopy which can be executed without exception but still nothing in the database.

Some troubleshooting done:

  • If my table is without any schema, it works. I can execute without exception and in the database I can see the data.
  • But if my table is with schema, I can execute it without exception but in the database, I cannot see the data.

Model with Schema

[Table("Person", Schema = "Persons")]
public partial class Person
{
    [Key]
    public int Id { get; set; }
}

Model without Schema

[Table("Person")]
public partial class Person
{
    [Key]
    public int Id { get; set; }
}

What have I miss out? How to troubleshoot further?

Steve
  • 2,963
  • 15
  • 61
  • 133
  • 1
    Could you please try non async version of BulkCopy? – Svyatoslav Danyliv Jan 28 '21 at 07:35
  • 1
    And why you have specified TableName? Also there is SchemaName, so TableName should not contain any quotes and schema. – Svyatoslav Danyliv Jan 28 '21 at 07:40
  • If I just put `TableName = "Person"`, I get error "Cannot access destination table '[Person]'. I tried with non async but still no data in the database after executed – Steve Jan 28 '21 at 07:43
  • 1
    Ok, you have used function outside EF, so linq2db do not know how to map to objects to database. linq2db has own mapping - you have use them in that case. But better `ctx.BulkCopy` – Svyatoslav Danyliv Jan 28 '21 at 07:53
  • I also used db.BulkCopy. I can execute without error but no data in the database. – Steve Jan 28 '21 at 08:01
  • 1
    First time i hear that BulkCopy have not inserted data. What shown in log? – Svyatoslav Danyliv Jan 28 '21 at 08:18
  • @SvyatoslavDanyliv, if my table does not have schema, the execution works and I can see the data in the database. Will your library works for schema? – Steve Jan 28 '21 at 09:11
  • 1
    It should work. EF integration should read schema info from Model. Also BulkCopyOptions have SchemaName property and a lot of other tweaks. – Svyatoslav Danyliv Jan 28 '21 at 10:17
  • @SvyatoslavDanyliv, have you tested your library with Schema in SQL Server database? I have posted my code for the model. Even my model is just one column, with schema, I can't see the save saved in the database. – Steve Jan 28 '21 at 10:42
  • For sure we have tested that with SQL Server. We have full CI cycle which runs about 400K tests. Check maybe you have started transaction and not committed and changes just rolled back. – Svyatoslav Danyliv Jan 28 '21 at 10:53

0 Answers0