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?