0

I am building a finance application and for the sake of storing stock prices I am utilizing the following class:

public sealed class Price
{
    public long CompanyId { get; set; }
    
    public long PriceId { get; set; }
    
    public DateTime TimeStamp { get; set; }
    
    public double Value { get; set; }
}

generated database context scaffolded with ef core from SQLite is as follows:

modelBuilder.Entity<Price>(entity =>
        {
            entity.HasKey(e => new { e.CompanyId, e.PriceId });

            entity.Property(e => e.TimeStamp).IsRequired();

            entity.Property(e => e.Value)
                .IsRequired()
                .HasColumnType("REAL");
        });

and now I am trying to add a record in such a manner that I am specifying the CompanyId and ef core autoincrements PriceId. For this purpose I have changed the model builder:

modelBuilder.Entity<Price>(entity =>
        {
            entity.HasKey(e => new { e.CompanyId });

            entity.Property(e => e.PriceId).ValueGeneratedOnAdd();

            entity.Property(e => e.TimeStamp).IsRequired();

            entity.Property(e => e.Value)
                .IsRequired()
                .HasColumnType("REAL");
        });

However, whenever I am trying to add a record:

Context.Prices.Add(new Price{CompanyId = 1, Value = 1234.56, TimeStamp = DateTime.Now});

the SqliteException: SQLite Error 19: 'NOT NULL constraint failed: Prices.PriceId'. exception is being thrown. What am I doing wrong?

Artur
  • 1
  • 2
  • Do you generate and execute a migration after updating the mapping? – Andriy Shevchenko Oct 23 '21 at 09:37
  • At first I created a database and then scaffolded it. Now I am making a new migration and making and executing it but result is the same. – Artur Oct 23 '21 at 13:14
  • Can you try to delete a db, delete all migration files, including `DbContextSnapshot` and scaffold db basically from scratch? I assume you use code first? – Andriy Shevchenko Oct 23 '21 at 13:40
  • If I delete a db whats to scaffold then? I dont think I understand you correctly :/ Considering code first I have a concrete generated model that I would like to implement into database. Forgive me for my inexpierience I am a beginner when comes to ef core. – Artur Oct 23 '21 at 13:48
  • Could you please share some code where you create `DbContext` and use it later? Is it a desktop app? Do you use dependency injection? – Andriy Shevchenko Oct 23 '21 at 14:02
  • First I have created database with SQLite browser. It's tables and columns. Then I scaffolded it. I am basing on the Context.cs and models generated by this scaffold. It's a web api app. – Artur Oct 23 '21 at 14:12
  • Then make sure you call `DbContext.Database.Migrate()` in `Startup` class – Andriy Shevchenko Oct 23 '21 at 14:23
  • All the same, NOT NULL constraint fails :/ – Artur Oct 23 '21 at 14:37
  • Artur if it's not for work could please share a link to repository? I'd like to try out myself – Andriy Shevchenko Oct 23 '21 at 15:30
  • https://github.com/ArturMarekNowak/FinanceWebApp/tree/feature/RequestingService – Artur Oct 23 '21 at 16:03
  • It seems to be an unfixed bug: https://stackoverflow.com/questions/49592274/how-to-create-autoincrement-column-in-sqlite-using-ef-core – Artur Oct 31 '21 at 14:54

0 Answers0