0

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.

  • EF doesn't assign IDs, it merely defaults to expect (and create if code first) identity columns for PKs that follow the *Id naming convention. PKs should not be null-able types, so that ShipCarrierId should be an `int` rather than `int?`. If you want an auto-incrementing ID you will need to set up the identity/sequence manually. (for SQL Server see: https://stackoverflow.com/questions/5974554/ef-code-first-how-to-set-identity-seed) If you don't want an auto-increment then use `[DatabaseGenerated(DatabaseGeneratedOption.None)]` on the key column. – Steve Py Feb 09 '22 at 10:11
  • @StevePy Thanks, after some more testing I found that the error has to be in the rest of the class. When I use this reduced model the problem does not occur. I will post an answer after finding the source of this problem. – Tobias Mikutta Feb 09 '22 at 11:02

1 Answers1

0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 15 '22 at 07:25