I am using Entity Framework 6
with Oracle
. The database is from a legacy system, so I plan to set the primary registry keys on hand.
To make life easier, I want to get the SaveChanges
of DbContext
data from the table and the fields that are primary key to execute the SQL command below, thus avoiding conflicts.
Doubt
In my DbContext
, how do I get the attributes Table
and Key
and their respective values?
SQL
public int GetNextID(string field, string table)
{
return Database.SqlQuery<int>("select coalesce(max(" + field + "), 0) + 1 from " + table).First();
}
Class
[Table("CHAMADOS")]
public class Chamado
{
[Key]
[Column("CD_CODIGO")]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Codigo { get; set; }
[Obrigatorio]
[Column("CD_EM_CODIGO")]
public int Empresa { get; set; }
}
My DbContext
public class OracleDbContext : DbContext
{
public OracleDbContext()
: base("OracleDbContext")
{
Database.SetInitializer<OracleDbContext>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.HasDefaultSchema("");
}
public override int SaveChanges()
{
foreach (var registro in ChangeTracker.Entries())
{
if (registro.State == EntityState.Added)
{
// Gostaria de pegar aqui os dados do atributo Table e Key da classe
}
}
return base.SaveChanges();
}
public DbSet<Chamado> Chamado { get; set; }
}