private static ShowGenreRow MapObjToRow(ShowGenre item)
 {
     return new ShowGenreRow
     {
         ShowGenreId = item.ShowGenreId,
         ShowId = item.ShowId,
         GenreId = item.GenreId
     };
 }
 internal static void DeleteEntity(ShowGenre item, SqlConnection conn)
 {
     using (SqlCommand cmd = conn.CreateCommand())
     {
         cmd.CommandType = System.Data.CommandType.Text;
         cmd.CommandText = "delete ShowGenre where ShowGenreId = @ShowGenreId";
         cmd.Parameters.AddWithValue("@ShowGenreId", item.ShowGenreId);
         cmd.ExecuteNonQuery();
     }
 }
        internal static void InsertEntity(ShowGenre item, SqlConnection conn)
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandType = System.Data.CommandType.Text;
                var sql = new StringBuilder();
                sql.Append("insert ShowGenre (ShowId, GenreId)");
                sql.Append("values (@ShowId, @GenreId);");
                sql.Append("select cast ( scope_identity() as int);");
                cmd.CommandText = sql.ToString();

                SetCommonParameters(item, cmd);
                item.ShowGenreId = (int)cmd.ExecuteScalar();
            }
        }
        internal static void UpdateEntity(ShowGenre item, SqlConnection conn)
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                cmd.CommandType = System.Data.CommandType.Text;
                var sql = new StringBuilder();
                sql.Append("update ShowGenre set ");
                sql.Append(" ShowId = @ShowId, ");
                sql.Append(" GenreId = @GenreId ");
                sql.Append("where ShowGenreId = @ShowGenreId");
                cmd.CommandText = sql.ToString();

                SetCommonParameters(item, cmd);
                cmd.Parameters.AddWithValue("@ShowGenreId", item.ShowGenreId);

                cmd.ExecuteNonQuery();
            }
        }
 public ShowGenre PersistChild(ShowGenre showGenre, SqlConnection conn)
 {
     if (showGenre.ShowGenreId == 0 && showGenre.IsMarkedForDeletion)
     {
         showGenre = null;
     }
     else if (showGenre.IsMarkedForDeletion)
     {
         DeleteEntity(showGenre, conn);
         showGenre = null;
     }
     else if (showGenre.ShowGenreId == 0)
     {
         InsertEntity(showGenre, conn);
         showGenre.IsDirty = false;
     }
     else if (showGenre.IsDirty)
     {
         UpdateEntity(showGenre, conn);
         showGenre.IsDirty = false;
     }
     return showGenre;
 }
 internal static ShowGenre Persist(ShowGenre item)
 {
     if (item.ShowGenreId == 0 && item.IsMarkedForDeletion) return null;
     if (item.ShowGenreId == 0)
     {
         // Insert
         var maxId = FakeDatabase.Instance
             .ShowGenres.Max(o => o.ShowGenreId);
         item.ShowGenreId = ++maxId;
         var row = MapObjToRow(item);
         FakeDatabase.Instance.ShowGenres.Add(row);
     }
     else
     {
         var existingRow = FakeDatabase.Instance.ShowGenres
             .FirstOrDefault(o => o.ShowGenreId == item.ShowGenreId);
         if (existingRow == null)
         {
             throw new ApplicationException(
                 "Record not found, another user may have deleted it.");
         }
         else if (item.IsMarkedForDeletion)
         {
             // Delete
             FakeDatabase.Instance.ShowGenres.Remove(existingRow);
         }
         else
         {
             // Update
             existingRow.ShowId = item.ShowId;
             existingRow.GenreId = item.GenreId;
         }
     }
     // reset dirty flag because obj is now synced with db
     if (item != null) item.IsDirty = false;
     return item;
 }
 private static ShowGenre MapRowToObj(ShowGenreRow row)
 {
     var existingItem = new ShowGenre
     {
         ShowGenreId = row.ShowGenreId,
         ShowId = row.ShowId,
         GenreId = row.GenreId
     };
     existingItem.IsDirty = false;
     return existingItem;
 }
 private static void SetCommonParameters(ShowGenre item, SqlCommand cmd)
 {
     cmd.Parameters.AddWithValue("@ShowId", item.ShowId);
     cmd.Parameters.AddWithValue("@GenreId", item.GenreId);
 }