This project is read-only.

Problems with update by OID

Nov 26, 2014 at 7:30 PM
Hi,

I am trying to update an item in the database by its OID.
But everytime I store the updated object NDatabase create a new object.
So I have thousands of duplicates.


My "update" method:
        private void UpdateTrans(LogItem lItem)
        {
            try
            {
                using (var odb = OdbFactory.Open(fDBFileName))
                {
                    LogItem itemFound = (from reg in odb.AsQueryable<LogItem>() where reg.OID == lItem.OID select reg).ToList<LogItem>()[0];
                    itemFound = lItem;
                    odb.Store(itemFound);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
The LogItem class:
    public class LogItem
    {
        // Object ID
        [OID]
        private readonly long _oid;

        public long OID
        {
            get { return _oid; }
        }

        public string SessionID { get; set; }
        // Item Header/Message
        public string Header { get; set; }

        // Creation DateTime
        public DateTime Date { get; set; }

        // Tries
        private int fRepeats = 1;

        public int Repeats
        {
            get { return fRepeats; }
            set { fRepeats = value; }
        }

        //Process
        private string fId = "";
        public string Id
        {
            get { return fId; }
            set { fId = value; }
        }

        //public List<ItemDetail> Details = new List<ItemDetail>();

        private string fServerId = "";
        public string ServerId
        {
            get { return fServerId; }
            set { fServerId = value; }
        }

        private DateTime fStarted;
        public DateTime Started { get { return fStarted; } }

        private DateTime fFinalized;
        private DateTime Finalized { get { return fFinalized; } }

        // Errors, Warnings and Infos
        private int fErrors = 0;
        public int Errors
        {
            get { return fErrors; }
            set { fErrors = value; }
        }

        private int fWarns = 0;
        public int Warns
        {
            get { return fWarns; }
            set { fWarns = value; }
        }

        private int fInfos = 0;
        public int Infos
        {
            get { return fInfos; }
            set { fInfos = value; }
        }

        public long Total = 0;
        public long Current = 0;

        public string Step = "";


        //Status Control
        public LogItemStatus Status = LogItemStatus.Paused;

        public void Start(long total, long current)
        {
            Total = total;
            Current = current;
            Status = LogItemStatus.Running;
        }

        public void Finish()
        {
            Status = LogItemStatus.Finished;
        }

        public LogItem()
        {

        }

        public LogItem(LogItemType lType, string lHeader, string lSessionID)
        {
            Date = DateTime.Now;
            Header = lHeader;
            SessionID = lSessionID;
            if (lType == LogItemType.Process)
                fId = Utils.CreateId();

            Id = fId;
        }

    }
Dec 14, 2014 at 5:01 PM
As I can see, you are replacing reference with your new object. NDatabase is able to recognize object based on reference if it's taken from database or not (some internal cache), so you cannot just replace reference and save it. Once you have object back from db, you need to put the same reference back after updating all fields you want to change.

Regards,
Jacek