Using EfCore 2.2.4 I am attempted to override the base DbSet
implementation in one of my Db Contexts.
I am using Oracle.ManagedDataAccess.Core
2.18.6 and if I try to call context.Customer.Add(new Customer());
(All fields in customer are nullable for arguments sake except the primary key RECORDNO
) I get the error:
cannot insert NULL into (SCHEMA.CUSTOMER.RECORDNO).
I realise I need to work out and add the RECORDNO
sequence value to the entity before I commit changes via EfCore and wanted to do this by extending DbSet.Add
as follows:
public class OracleDbContext : DbContext
{
public class OracleDbSet<TEntity> : DbSet<TEntity> where TEntity : class
{
public override EntityEntry<TEntity> Add(TEntity entity)
{
SetEntityRecordNumber(entity); //this will use reflection for [Key] attribute and set it to a sequence value I collect from the database
return base.Add(entity);
}
}
public OracleDbSet<Customer> Customers { get; set; }
...
However when I now call context.Customer.Add(new Customer());
the value is null for context.Customer
.
How do I properly get the DbContext
to register instances of OracleDbContext
aswell as DbContext
, or is there a simpler and still generic way of achieving an override or extension to DbContext.Add()
?