This project is read-only.

Md5Concurrency error

Jun 4, 2010 at 3:52 AM

A concurrency exception happened: Checksums different.

I’m having trouble persisting changes, I’m using context to retrieve a Booking record from the database, I then change the start date then using the same instance of context I persist the booking which the throws a concurrency exception.

Simplified code example:

public void UpdateBooking(int id, Datetime date){

            Booking booking =  Context.GetObjectSearcher<Booking>().FindOne("ID = {0}",id)

            booking.StartDate = date

            Context.PersistChanges(booking);

}

This is running on my local machine so I know there has not been any changes to the record between when I retrieve the record from the db and when I call PersistChanges

Jun 6, 2010 at 9:18 PM

What database are you using?

Jun 7, 2010 at 2:50 AM

Cosmic Distortion is using MS SQL 2008

Jun 7, 2010 at 2:55 AM

It may also be important for you to know that is this for an asp.net application where context factory returns a new instance of context for every call and that all opf3 wizard generated classes  implement ISelfContaining w- ObjectInfo for when the object are stored in session or profile in order to maintain state.

Jun 8, 2010 at 4:38 AM

Any chance to post the persistent object?

Jun 9, 2010 at 12:55 AM

This is the class we are trying to persist:

 

#region Developer's comment header
/* Booking.cs
 *
 * author:    Roster Team (Michael - Erwin)
 * copyright: © 2010 -. All rights reserved.
 *
 * history:   5/28/2010, Roster Team (Michael - Erwin): Created.
 *
 */
#endregion
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;

using Chili.Opf3;
using Chili.Opf3.Relations;


#endregion

namespace Domain
{
    /// <summary>
    /// Persistent object for the "Booking" table.
    /// </summary>
    [Serializable]
    [Persistent("Booking")]
    public class Booking : Business_Object, IDynamicExtensible, IObjectNotification, ISelfContainingObject, INotifyPropertyChanged, IBooking
    {
        #region Fields

        private int _id;
        private int _subjectID;
        private int _roomID;
        private int _timetableID;
        private int _teacherID;
        private int _studentClassID;
        private DateTime _startTime;
        private DateTime _endTime;

        #endregion

        #region Relations

        [Relation("RoomID = ID", PersistRelationship = PersistRelationships.ChildFirst)]
        private ObjectHolder<Room> _room = new ObjectHolder<Room>();

        /// <summary>
        /// Specifies the connected "Room" persistent object.
        /// </summary>
        [XmlIgnore]
        public Room Room
        {
            get { return _room.InnerObject; }
            set { _room.InnerObject = value; }
        }

        [Relation("StudentClassID = ID", PersistRelationship = PersistRelationships.ChildFirst)]
        private ObjectHolder<StudentClass> _studentClas = new ObjectHolder<StudentClass>();

        /// <summary>
        /// Specifies the connected "StudentClas" persistent object.
        /// </summary>
        [XmlIgnore]
        public StudentClass StudentClass
        {
            get { return _studentClas.InnerObject; }
            set { _studentClas.InnerObject = value; }
        }

        [Relation("SubjectID = ID", PersistRelationship = PersistRelationships.ChildFirst)]
        private ObjectHolder<Subject> _subject = new ObjectHolder<Subject>();

        /// <summary>
        /// Specifies the connected "Subject" persistent object.
        /// </summary>
        [XmlIgnore]
        public Subject Subject
        {
            get { return _subject.InnerObject; }
            set { _subject.InnerObject = value; }
        }

        [Relation("TeacherID = ID", PersistRelationship = PersistRelationships.ChildFirst)]
        private ObjectHolder<Teacher> _teacher = new ObjectHolder<Teacher>();

        /// <summary>
        /// Specifies the connected "Teacher" persistent object.
        /// </summary>
        [XmlIgnore]
        public Teacher Teacher
        {
            get { return _teacher.InnerObject; }
            set { _teacher.InnerObject = value; }
        }

        [Relation("TimetableID = ID", PersistRelationship = PersistRelationships.ChildFirst)]
        private ObjectHolder<Timetable> _timetable = new ObjectHolder<Timetable>();

        /// <summary>
        /// Specifies the connected "Timetable" persistent object.
        /// </summary>
        [XmlIgnore]
        public Timetable Timetable
        {
            get { return _timetable.InnerObject; }
            set { _timetable.InnerObject = value; }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Returns the identifier of the persistent object. Don't set it manually!
        /// </summary>
        [Field("ID", AllowDBNull = false, Identifier = true, AutoNumber = true)]
        public int ID
        {
            get
            {
                return _id;
            }
            private set
            {
                _id = value;

                OnRowChanged("ID");
            }
        }

        /// <summary>
        /// The foreign key connected with another persistent object.
        /// </summary>
        [Field("SubjectID", AllowDBNull = false)]
        public int SubjectID
        {
            get
            {
                return _subjectID;
            }
            private set
            {
                _subjectID = value;

                OnRowChanged("SubjectID");
            }
        }

        /// <summary>
        /// The foreign key connected with another persistent object.
        /// </summary>
        [Field("RoomID", AllowDBNull = false)]
        public int RoomID
        {
            get
            {
                return _roomID;
            }
            private set
            {
                _roomID = value;

                OnRowChanged("RoomID");
            }
        }

        /// <summary>
        /// The foreign key connected with another persistent object.
        /// </summary>
        [Field("TimetableID", AllowDBNull = false)]
        public int TimetableID
        {
            get
            {
                return _timetableID;
            }
            private set
            {
                _timetableID = value;

                OnRowChanged("TimetableID");
            }
        }

        /// <summary>
        /// The foreign key connected with another persistent object.
        /// </summary>
        [Field("TeacherID", AllowDBNull = false)]
        public int TeacherID
        {
            get
            {
                return _teacherID;
            }
            private set
            {
                _teacherID = value;

                OnRowChanged("TeacherID");
            }
        }

        /// <summary>
        /// The foreign key connected with another persistent object.
        /// </summary>
        [Field("StudentClassID", AllowDBNull = false)]
        public int StudentClassID
        {
            get
            {
                return _studentClassID;
            }
            private set
            {
                _studentClassID = value;

                OnRowChanged("StudentClassID");
            }
        }

        /// <summary>
        /// This property is mapped to the "StartTime" field.  Mandatory.
        /// Storage comment: .
        /// </summary>
        [Field("StartTime", AllowDBNull = false)]
        public DateTime StartTime
        {
            get
            {
                return _startTime;
            }
            set
            {
                _startTime = value;

                OnRowChanged("StartTime");
            }
        }

        /// <summary>
        /// This property is mapped to the "EndTime" field.  Mandatory.
        /// Storage comment: .
        /// </summary>
        [Field("EndTime", AllowDBNull = false)]
        public DateTime EndTime
        {
            get
            {
                return _endTime;
            }
            set
            {
                _endTime = value;

                OnRowChanged("EndTime");
            }
        }

        #endregion

        #region Methods and Constructors

        private Booking() {}

        public Booking(Timetable timetable, DateTime startTime, DateTime endTime, DayOfWeek dayOfWeek,
                Room room, Subject subject, Teacher teacher, StudentClass studentClass)
        {
            StartTime = timetable.StartDate;
            EndTime = timetable.StartDate;
            SetStartTime(startTime);
            SetEndTime(endTime);
            MoveDateInWeekTo(dayOfWeek);
            this.Room = room;
            this.Timetable = timetable;
            this.Subject = subject;
            this.Teacher = teacher;
            this.StudentClass = studentClass;
        }

        public static Booking GetBooking(int id) {
            return StorageObject.GetModel<Booking>(id);
        }

        public void MoveDateInWeekTo(DayOfWeek dayOfWeek) {
            int day = (int)StartTime.DayOfWeek;
            int newDay = (int)dayOfWeek;
            int difference = newDay - day;
            StartTime = StartTime.AddDays(difference);
            EndTime = EndTime.AddDays(difference);
        }

        public void SetStartTime(DateTime time) {
            StartTime = SetTime(StartTime, time);
        }

        public void SetEndTime(DateTime time) {
            EndTime = SetTime(EndTime, time);
        }

        private DateTime SetTime(DateTime oldtime, DateTime newtime) {
            DateTime time = new DateTime(
                oldtime.Year, oldtime.Month, oldtime.Day,
                newtime.Hour, newtime.Minute, oldtime.Second, oldtime.Millisecond);

            return time;
        }

        public BookingValidator.ValidationResult ValidateBooking(ObjectContext context) {
            return new BookingValidator(context, this).Validate();
        }

        #endregion

        #region IDynamicExtensible Members

        private IDictionary<string, object> _unknownfields = new Dictionary<string, object>();

        /// <summary>
        /// Specifies the unknown fields of the persistent object.
        /// </summary>
        public IDictionary<string, object> UnknownFields
        {
            get
            {
                return _unknownfields;
            }
        }

        #endregion

        #region IObjectNotification Members

        /// <summary>
        /// Invoked by the framework before the persistent object is populated.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnBeforePopulated(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        /// <summary>
        /// Invoked by the framework after the object is populated.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnAfterPopulated(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        /// <summary>
        /// Invoked by the framework before the object is inserted.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnBeforeInsert(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        /// <summary>
        /// Invoked by the framework after the object is inserted.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnAfterInsert(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        /// <summary>
        /// Invoked by the framework before the object is updated.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnBeforeUpdate(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        /// <summary>
        /// Invoked by the framework after the object is updated.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnAfterUpdate(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        /// <summary>
        /// Invoked by the framework before the object is deleted.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnBeforeDelete(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        /// <summary>
        /// Invoked by the framework after the object is deleted.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnAfterDelete(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        /// <summary>
        /// Invoked by the framework after the object has been marked deleted.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnBeforeMarkDeleted(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        /// <summary>
        /// Invoked by the framework after the object has been marked deleted.
        /// </summary>
        /// <param name="e">
        /// Contain information about the state of the persistent object and the
        /// <see cref="Opf3.ObjectContext">ObjectContext</see> of the current operation.
        /// </param>
        void IObjectNotification.OnAfterMarkDeleted(ObjectNotificationEventArgs e)
        {
            // Add code here, if required. Leave empty if the event is not required.
        }

        #endregion

        #region ISelfContainingObject Members

        private ObjectInfo _objectInfo = null;

        /// <summary>
        /// Returns the <see cref="Opf3.ObjectInfo">ObjectInfo</see> object that contains all
        /// information required to make the persistent object
        /// <see cref="Opf3.ObjectContext">independent from any ObjectContext</see>.
        /// </summary>
        /// <value>
        ///     <see cref="Opf3.ObjectInfo">ObjectInfo</see> object that holds the status
        /// information of the persistent object.
        /// </value>
        public ObjectInfo ObjectInfo
        {
            get { return _objectInfo; }
            set { _objectInfo = value; }
        }

        #endregion

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Invokes the "PropertyChanged" event if a property has been changed and
        /// somebody subscribed the event.
        /// </summary>
        private void OnRowChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// Event is fired if a property of the persistent changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}