c# - Save a related entry 'on-the-fly' if it does not already exist -


this seems simple, yet can't head around how should it. keep list of 'position' names of employees. so, when entering employee, should able choose either list of positions, or enter new one. in end should of course store id of position in position table.

tables this

public class position {     public int id {get; set;}     public string name {get;set;} }  public class employee {     public int id {get; set;}     public string lastname {get;set;}     :     :     public int positionid {get;set} }  

i think having type-ahead function looks existing entries type, , in case no match found, post new position position table , somehow newly created id hereof , store in employee record. done - specifically, there way return key of newly created record?

hmm, might have explained myself solution - general way of doing it?

thanks :)

i agree user1987322 may not best solution, in case:

if using entity framework associate 1 entity related entity. model this:

using(var context = new entitycontext()) {     employee emp = new employee()     {        lastname = lastname,        position = new position { name = name }     };      context.employees.addobject(emp);     context.savechanges(); } 

the new position entity brought context new employee, , association fix-up taken care of framework.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -