1

This is in reference to the question I asked regarding how to determine when items are added to the virtual ICollection property. As suggested, I have created a custom collection which inherits from Collection as shown below

public class EntityCollection<T> : Collection<T>
{
    protected override void InsertItem(int index, T item)
    {
        base.InsertItem(index, item);
    }
}

This is being used as

public class DbAppointment
{
    public DbAppointment()
    {
        exceptionOcurrences = new EntityCollection<DbExceptionOcurrence>();
    }

    public virtual int AppointmentId { get; set; }
    public virtual string Subject { get; set; }
    public virtual string Body { get; set; }
    public virtual DateTime Start { get; set; }
    public virtual DateTime End { get; set; }

    private ICollection<DbExceptionOcurrence> exceptionOcurrences;
    public virtual ICollection<DbExceptionOcurrence> ExceptionOcurrences
    {
        get { return exceptionOcurrences; }
        set { exceptionOcurrences = value; }
    }
}

The problem is the only time the overridden InsertItem method seems to get called is if I initialise the database with a custom initialiser (example code below) and override the seed method!! What am I doing wrong?

Cheers Abs

public class ContextInitializer : DropCreateDatabaseAlways<Context>
{
    protected override void Seed(Context context)
    {
        new List<DbAppointment>
        {
            new DbAppointment{ Subject = "hello", Body="world", Start=DateTime.Now,  End=DateTime.Now.AddMinutes(30)},

        }.ForEach(a => context.Appointments.Add(a));

        new List<DbExceptionOcurrence>
        {
            new DbExceptionOcurrence{ExceptionDate=DateTime.Now}
        }.ForEach(eo => context.ExceptionOcurrences.Add(eo));

        base.Seed(context);
    }
}
Community
  • 1
  • 1
user1460473
  • 115
  • 9
  • 1
    Can you try to replace `ICollection` in the class definition by `EntityCollection` and test if it helps? And: the name `EntityCollection` is not the best choice because EF already has a type with that name... not that your constructor uses that type because of some `using` statements in the class file... – Slauma Jun 17 '12 at 14:33

0 Answers0