简体   繁体   English

EF Core 5 byte[] null 在数据库中

[英]EF Core 5 byte[] null in database

I´m creating a WebAPI for an already existing database.我正在为一个已经存在的数据库创建一个 WebAPI。 In one table, the primary key column starts at -1.在一个表中,主键列从 -1 开始。 When I call当我打电话

            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);

In

var test =await Contexts.CustomerDbContext.ShipCarriers.ToListAsync(); var test =await Contexts.CustomerDbContext.ShipCarriers.ToListAsync();

the first two entries are null and for前两个条目是 null 和

var test2 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == 0); var test2 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == 0);

var test3 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == -1); var test3 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == -1);

both are null.都是 null。

Here are the class and the configuration I´m using这是 class 和我使用的配置

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; }
    
}

As far as I found the Entity-Framework, in default, starts PKs at 1 and handles values 0 and -1 as invalid keys.据我所知,实体框架在默认情况下从 1 开始 PK,并将值 0 和 -1 处理为无效键。 Is there a way to change this behavior?有没有办法改变这种行为?

EDIT: I found what was causing the problem.编辑:我发现是什么导致了问题。 In the database the fields for在数据库中的字段

[Column("logoform")]
public byte[] LogoForm { get; set; }

[Column("logobutton")]
public byte[] LogoButton { get; set; }

are null for only these two entries.只有这两个条目是 null。 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.我使用的是 PostgreSQL 数据库,两者的字段类型都是 bytea。

As I could not get EF to read the null values from my DB into the byte[] I added a 1x1px transparent image into the database.由于我无法让 EF 将 null 值从我的数据库读取到 byte[] 中,因此我在数据库中添加了一个 1x1px 透明图像。

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

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