Massive.DynamicModel.Update C# (CSharp) Method

Update() public method

Updates a record in the database. You can pass in an Anonymous object, an ExpandoObject, A regular old POCO, or a NameValueCollection from a Request.Form or Request.QueryString
public Update ( object o, object key ) : int
o object
key object
return int
        public virtual int Update(object o, object key)
        {
            var ex = o.ToExpando();
            if (!IsValid(ex)) {
                throw new InvalidOperationException("Can't Update: " + String.Join("; ", Errors.ToArray()));
            }
            var result = 0;
            if (BeforeSave(ex)) {
                result = Execute(CreateUpdateCommand(ex, key));
                Updated(ex);
            }
            return result;
        }

Usage Example

        public static void ScrapeChannels(object args)
        {
            if (Interlocked.CompareExchange(ref Running, 1, 0) != 0)
                return;

            try
            {
                if (String.IsNullOrEmpty(downloadDir))
                {
                    string dir = ConfigurationManager.AppSettings["torrentsdir"];
                    if (!String.IsNullOrEmpty(dir))
                    {
                        var context = args as HttpContextBase;
                        if (Path.IsPathRooted(dir) == false && context != null)
                            dir = context.Server.MapPath(dir);

                        if (Directory.Exists(dir))
                            downloadDir = dir;
                        else
                            downloadDir = Environment.CurrentDirectory;
                    }
                }

                var table = new DynamicModel("LDSTorrents", tableName: "Channels", primaryKeyField: "ChannelID");

                var channels = table.All();
                foreach (var channel in channels)
                {
                    try
                    {
                        logger.InfoFormat("Scraping '{0}'...", channel.Title);
                        var videos = ScrapeChannel(channel);
                        Torrents.Save(videos.ToArray());
                        table.Update(new { LastUpdated = DateTime.Now }, channel.ChannelID);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(String.Format("Failed to scrape channel '{0}' with the following exception: ", channel.Title), ex);
                        ErrorLog.GetDefault(null).Log(new Error(ex));
                    }
            #if DEBUG
                    break;
            #endif
                }
            }
            catch (Exception ex)
            {
                logger.Error("ScrapeChannels failed with the following exception: ", ex);
                ErrorLog.GetDefault(null).Log(new Error(ex));
            }

            Interlocked.Exchange(ref Running, 0);
        }
All Usage Examples Of Massive.DynamicModel::Update