locked
Code First RRS feed

  • Question

  • I have 3 class Task, Work, SpentTime

    Task must have Required SpentTime

    And  Work must have Required SpentTime

    SpentTime must have Optional Work, and Optional Task

    I have realize cascade delete SpentTime when i remove Work or Task

           

     public class Task
        {
            public int TaskId { get; set; }

            public string Discription { get; set; }      

            public SpentTime SpendTimeForTask { get; set; }

            public Work Work { get; set; }     

        }

    public class Work
        {
            public int WorkId { get; set; }
           
            public SpentTime SpendTimeForWork { get; set; }

            public List<Task> TaskList { get; set; }
           
        }

                 

     public class SpentTime
        {
            public int SpentTimeId { get; set; }

            public DateTime AssumedStart { get; set; }

            public Task Task { get; set; }

            public Work Work { get; set; }

        }


    public TaskConfig() {
                 
                Property(p => p.Discription).IsRequired().HasMaxLength(100);
                HasRequired(p => p.SpendTimeForTask).WithOptional(p => p.Task);
            }

     public SpentTimeConfig() {
                HasKey(p => p.SpentTimeId);
                HasOptional(p => p.Task).WithRequired(p => p.SpendTimeForTask).WillCascadeOnDelete(true);
                HasOptional(p => p.Work).WithRequired(p => p.SpendTimeForWork).WillCascadeOnDelete(true);
            }

     public WorkConfig()
            {
               
                Property(p => p.TypeWork).IsRequired().HasMaxLength(50);           

            }


    db.Tasks.Include(p=>p.SpendTimeForTask).ToList();

    when I call ToList I have Exeption 

    Additional information: Multiplicity constraint violated. The role 'SpentTime_Task_Target' of the relationship 'EntityFramework.CodeFirst.SpentTime_Task' has multiplicity 1 or 0..1.

    What happen??

    And When i call this query

     var query1 = from q in db.Tasks.Local where item.WorkList.Contains(q.Work) select q;

     I have Exeption Additional information: A referential integrity constraint violation occurred: A primary key property that is a part of referential integrity constraint cannot be changed when the dependent object is Unchanged unless it is being set to the association's principal object. The principal object must be tracked and not marked for deletion.



    • Edited by Ahlompys Saturday, October 29, 2016 11:01 AM
    Saturday, October 29, 2016 10:58 AM

All replies

  • Hi Ahlompys

    >> Additional information: Multiplicity constraint violated. The role 'SpentTime_Task_Target' of the relationship 'EntityFramework.CodeFirst.SpentTime_Task' has multiplicity 1 or 0..1.

    According to your code,  the task and the SpentTime is the one-to-zero-or-one relationship, and you configed a same relationship twice for task and SpentTime.  

    This line should not be necessary, Try again removing this line.

    >> HasRequired(p => p.SpendTimeForTask).WithOptional(p => p.Task);

    As the following line should be enough to set the foreign key.

    >> HasOptional(p => p.Task).WithRequired(p => p.SpendTimeForTask).WillCascadeOnDelete(true);

    The framework keep tracks of the containing objects by their references this is why can’t have two object (an entity or an entity's navigation property which is a collection) the same reference. If one entity has a collection of other related entities the collection can’t be attached to another with the same collection you need to create a whole new list of them

    Best Regards,

    Tuesday, November 1, 2016 10:04 AM