In my postgres database, the keys for my tables are serial data types that the postgres database autoincrements. Using C#, Npgsql, Entity Framework 6, WPF, linq and EF dbsets (any extension methods?), is there a way to implement a repository method such as:
public virtual void Add(TEntity entity, string addrow)
{NpgsqlCommand cmd = new NpgsqlCommand(addrow, DbConnect);
cmd.ExecuteNonQuery();}
The viewmodel would pass in the string argument something like:
_addRow = string.Format("insert into mytable ('descrip') values ( " + DescripProperty + ") returning 'mytable_id' into mytableID");
Is this the right way to insert a postgres row / record that has an autoincrement key? Or do I need to utilize a PL/pgSQL - SQL Procedural Language function to first lock the table and do the insert from that server side? Thank you in advance.
EDIT --------------------------- For clarification, I pass the EF DbContext into the genericrepository constructor. This code is in genericrepository class:
private readonly DbSet<TEntity> _aquery;
public DbSet<TEntity> AQuery;
// CTOR - inject dbcontext/entities
public GenericRepository(MyContextClass context)
{
if (context == null)
throw new ArgumentNullException("context");
_theDbContext = context;
_aquery = _theDbContext.Set<TEntity>();
AQuery = _aquery;
}
So I can do AQuery.AsQueryable, etc.