1

这是参考我提出的关于如何确定何时将项目添加到虚拟 ICollection 属性的问题。如建议的那样,我创建了一个继承自 Collection 的自定义集合,如下所示

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

这被用作

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; }
    }
}

问题是唯一一次似乎调用了被覆盖的 InsertItem 方法是如果我使用自定义初始化程序(下面的示例代码)初始化数据库并覆盖种子方法!我究竟做错了什么?

干杯腹肌

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);
    }
}
4

0 回答 0