I am trying to use the bulk insert feature. My model looks like this:
public class Stockprice
{
[Key]
public int Id { get; set; }
[Required]
[LinqToDB.Mapping.Association(ThisKey = "StockId", OtherKey = "Id")]
public Stock Stock { get; set; }
//some more
}
public class MinuteStockprice : Stockprice { }
The code insert code:
private async Task SavePrices<T>(List<Quote> quotes) where T : Stockprice, new()
{
using var context = _factory.CreateDbContext();
var connection = context.CreateLinqToDbConnection();
connection.TraceSwitchConnection.Level = TraceLevel.Verbose;
connection.OnTraceConnection = x => Console.WriteLine(x.SqlText);
var stocks = new List<T>();
foreach (var quote in quotes)
{
var price = new T { Stock = new Stock() { Id = _isinToStockId[quote.Isin] }, Ask = quote.Ask, Bid = quote.Bid, Time = quote.Time };
stocks.Add(price);
context.Stocks.Attach(price.Stock);
}
await connection.BulkCopyAsync(stocks);
//await context.BulkCopyAsync(stocks);
}
Not sure what I am missing, but it doesn't insert the StockId. This is the head of the generated Sql:
INSERT INTO [MinuteStockprices]
(
[Time],
[Ask],
[Bid]
)
How do I include the StockId here?
EDIT
The table is created like this:
CREATE TABLE "MinuteStockprices" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_MinuteStockprices" PRIMARY KEY AUTOINCREMENT,
"StockId" INTEGER NOT NULL,
"Bid" REAL NOT NULL,
"Ask" REAL NOT NULL,
"Time" TEXT NOT NULL,
CONSTRAINT "FK_MinuteStockprices_Stocks_StockId" FOREIGN KEY ("StockId") REFERENCES "Stocks" ("Id") ON DELETE CASCADE
)