简体   繁体   English

Linq2Db:如何在(批量)插入上包含导航属性

[英]Linq2Db: How to include navigation properties on (bulk-)insert

I am trying to use the bulk insert feature.我正在尝试使用批量插入功能。 My model looks like this:我的 model 看起来像这样:

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.不知道我错过了什么,但它没有插入 StockId。 This is the head of the generated Sql:这是生成的 Sql 的头部:

INSERT INTO [MinuteStockprices]
(
        [Time],
        [Ask],
        [Bid]
)

How do I include the StockId here?如何在此处包含 StockId?

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
)

BulkCopy is low level command which inserts into ONE table and only in one table. BulkCopy是低级命令,它插入到一张表中,并且只在一张表中。 If you need to insert details, you have to do that separately.如果您需要插入详细信息,则必须单独进行。

Also check that class has StockId property, because shadow properties are not supported.还要检查 class 是否具有StockId属性,因为不支持阴影属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM