I´m creating a WebAPI for an already existing database. In one table, the primary key column starts at -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();
the first two entries are null and for
var test2 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == 0);
var test3 = await Contexts.CustomerDbContext.ShipCarriers.FirstOrDefaultAsync(x => x.ShipCarrierId == -1);
both are null.
Here are the class and the configuration I´m using
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. 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. 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.
CodePudding user response:
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.