我正在為一個已經存在的資料庫創建一個 WebAPI。在一個表中,主鍵列從 -1 開始。當我打電話
var test = await Contexts.CustomerDbContext.ShipCarriers.ToListAsync();
var test2 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == 0);
var test3 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == -1);
在
var test =await Contexts.CustomerDbContext.ShipCarriers.ToListAsync();
前兩個條目為 null 和 for
var test2 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == 0);
var test3 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == -1);
兩者都是空的。
這是我正在使用的課程和配置
public class ShipCarrierConfiguration : IEntityTypeConfiguration<ShipCarrierDbObject>
{
public void Configure(EntityTypeBuilder<ShipCarrierDbObject> builder)
{
}
}
[Table("spedition")]
public class ShipCarrierDbObject
{
[Key]
[Column("speditionid")]
public int? ShipCarrierId {get ;set ;}
[Column("cloudid")]
public string CloudId { get; set; }
[Column("bezeichnung")]
public string Name { get; set; }
[Column("logo")]
public byte[]? Logo { get; set; }
[Column("logoform")]
public byte[]? LogoForm { get; set; }
[Column("logobutton")]
public byte[]? LogoButton { get; set; }
}
據我發現,默認情況下,物體框架從 1 開始 PK,并將值 0 和 -1 處理為無效鍵。有沒有辦法改變這種行為?
編輯:我找到了導致問題的原因。在資料庫中的欄位
[Column("logoform")]
public byte[] LogoForm { get; set; }
[Column("logobutton")]
public byte[] LogoButton { get; set; }
are null for only these two entries. But I don′t understand why that prevents the entries from being loaded. I′m using a PostgreSQL database and the field type for both is bytea.
uj5u.com熱心網友回復:
由于我無法讓 EF 將我的資料庫中的空值讀取到 byte[] 中,因此我在資料庫中添加了一個 1x1px 透明影像。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426640.html
