Issue: I can't get the new ID of the inserted entry into my POSTGRES Database on DotNet Core. I am so depressed, i can't find this answer!
Here is the command that I am trying that DOES work when I use a postgres client. I am using RAW SQL commands to run the QUERY.
The SQL command that should work
context.Database.ExecuteSqlCommandAsync($"INSERT INTO pulserelations (pulseid, create_time, modified_time) VALUES ({pulse_id}, now(),now()) RETURNING pulserelationid;");
I have verified the insert was successful. The Return value of this function is always 1;
Additional Info
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql("Host=localhost;Database=localdb;Username=yyyy;Password=xxxx");
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
public DbQuery<IDInsertQuery> get_id_of_insert_query {get;set;}
I have tried to even RE-QUERY the database after an insert using and that didn't work either
public class IDInsertQuery
{
public int currval;
}
List<IDInsertQuery> list_id = await _context.get_id_of_insert_query.FromSql($"select currval from currval(pg_get_serial_sequence('pulserelations', 'pulserelationid'));").ToListAsync();