c# - Updating object ID with identity after saving to database -


i trying figure out best way update id of existing object after saved database.

e.g. in example below, contactdetailid in test method below 0 until physically again, update automatically item saved.

stepping through code below contactdetailid set once context.savechanges called , item saved database correctly, need push id ui.

what best way this?

    private void test()     {         int id = 0          contactdetail cd = new contactdetail();         cd.contacttypeid = 1;         cd.value = "3";         cd.save();          id = cd.contactdetailid;         }      public void save()     {         contactdetaildao service = new contactdetaildao();         contactdetaildto saveitem = new contactdetaildto();          if (isvalid(this))         {             saveitem.contactdetailid = this.contactdetailid;             saveitem.value = this.value;             saveitem.contacttypeid = this.contacttypeid;              service.save(saveitem);              this.contactdetailid = saveitem.contactdetailid;         }     }      public void save(contactdetaildto contactdetaildto)     {         if (contactdetaildto.isnew())         {             repository.add(new tblcontactdetail             {                 contactdetailid = contactdetaildto.contactdetailid,                 value = contactdetaildto.value,                 contacttypeid = contactdetaildto.contacttypeid             });         }     }      public virtual void add(t entity)     {         context.entry(entity).state = system.data.entitystate.added;          if (entity == null)         {             throw new argumentexception("cannot add null entity.");         }          this.context.set<t>().add(entity);         this.context.savechanges();     } 

or use this:

private void test() {     contactdetail cd = new contactdetail();     cd.contacttypeid = 1;     cd.value = "3";     cd.save();     return cd.contactdetailid;     }  public contactdetaildto save() {     contactdetaildao service = new contactdetaildao();     contactdetaildto saveitem = new contactdetaildto();      if (isvalid(this))     {         saveitem.contactdetailid = this.contactdetailid;         saveitem.value = this.value;         saveitem.contacttypeid = this.contacttypeid;          saveitem=service.save(saveitem);          this.contactdetailid = saveitem.contactdetailid;     }     return saveitem;  }  public contactdetaildto save(contactdetaildto contactdetaildto) {     if (contactdetaildto.isnew())     {         return repository.add(new tblcontactdetail         {             contactdetailid = contactdetaildto.contactdetailid,             value = contactdetaildto.value,             contacttypeid = contactdetaildto.contacttypeid         });     } }  public virtual t add(t entity) {     context.entry(entity).state = system.data.entitystate.added;      if (entity == null)     {         throw new argumentexception("cannot add null entity.");     }      this.context.set<t>().add(entity);     this.context.savechanges();     return entity; } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -