Search code examples
subsonicsubsonic3subsonic-active-record

Subsonic 3 - Insert of Update


Currently using Subsonic 3 with Activerecord. Purely out of curiosity, is there an easy way to update or insert a record in a clean and consise way?

Instead of

 var record = MyModal.SingleOrDefault(x => x.id == 1)
 if (record != null)
 {
     // code to update record here
 } else {
    // code to insert a record here
    MyModal record = new MyModal();
    record.attribute1 = "blah"
    ... blah blah
 }

Solution

  • The standard ActiveRecord templates provide a Save() method, which either calls Add() or Update() depending on whether the object IsNew(). It should work in the sample you gave.