I have the following table where I just added the decorator DatabaseGenerated like so:
public class Reference
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public decimal ReferenceId { get; set; }
public string AddressType { get; set; }
public string RefferenceType { get; set; }
public string RefferenceValue { get; set; }
[ForeignKey("Shipment")]
public decimal? TrackingNumber { get; set; }
public Shipment Shipment { get; set; }
}
And my latest up migration that is pushed to the oracle 19c DB is:
public override void Up()
{
DropPrimaryKey("SALOGSEARCH.References");
AlterColumn("SALOGSEARCH.References", "ReferenceId", c => c.Decimal(nullable: false, precision: 20, scale: 0, identity: true));
AddPrimaryKey("SALOGSEARCH.References", "ReferenceId");
}
Yet when I execute my context save after adding:
using (var context = new DbContext())
{
context.Reference.Add(shipperReference);
context.SaveChanges();
}
I get an exception
ORA-01400: cannot insert NULL
I assumed that the tag DatabaseGeneratedOption.Identity would generate a sequence in the Oracle database and call it from the default value on next or something. But that is not the case.
Do I have to manually create a trigger and sequence for each column now?
If so then what is the point the of code first if I have to meddle with the database myself.
I'm not sure if this has any effect but I am adjusting the OnModelCreating like so:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("SALOGSEARCH");
modelBuilder.Properties<string>().Configure(s => s.HasMaxLength(400).HasColumnType("Varchar2"));
modelBuilder.Properties<decimal>().Configure(s => s.HasPrecision(20, 0).HasColumnType("Number"));
}
Any pointers to solve this from c# side would be much appreciated.
EDIT1: Following this tutorial I've come to realize that the Id of the model class (table) needs to be an int which is number(10, 0) in oracle terms in order to automatically create the desired sequence (as the column default calling nextVal) on the DB side.
Looking at the up migration however, there doesn't seem to be any indication of this sequence creation, so it just leads me to believe that the creation happens somewhere deeper and outside of my field of knowledge.