EF 4.3(代码优先) - 确定项目何时添加到虚拟ICollection属性 [英] EF 4.3 (Code First) - determine when items are added to the virtual ICollection property

查看:138
本文介绍了EF 4.3(代码优先) - 确定项目何时添加到虚拟ICollection属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法确定实际项目何时从查询加载到ICollection<>虚拟成员?

Is there any way to determine when actual items are added to an ICollection<> virtual member when it is being loaded from a query?

希望以下代码将能够表明我的观点!!

Hopefully the code below will be able to demonstrate my point!!

public class DbAppointment
{
    public DbAppointment()
    {
    }

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

public class DbExceptionOcurrence
{
    public DbExceptionOcurrence()
    {
    }

    public virtual int ExceptionId { get; set; }
    public virtual int AppointmentId { get; set; }
    public virtual DateTime ExceptionDate { get; set; }
    public virtual DbAppointment DbAppointment { get; set; }
}

加载这些的代码是

        Database.SetInitializer(new ContextInitializer());
        var db = new Context("EFCodeFirst");

        // when this query executes the DbAppointment ExceptionOcurrenes (ICollection) is set
        // but for some reason I only see this as an empty collection in the virtual setter DbAppointment
        // once the query has completed I can see the ExceptionOcurrences
        var result = db.Appointments.Include(a => a.ExceptionOcurrences).ToList();

在每个项目的DbAppointment ICollection ExceptionOcurrences设置器中,我需要执行一些附加逻辑。我遇到的问题是,一旦DbAppointment对象已经被创建,我只会看到这些信息。

In the DbAppointment ICollection ExceptionOcurrences setter for each item I need to perform some addtional logic. The problem I am having is that I only seem to have this information once the DbAppointment objects have already been created.

有没有办法确定项目何时被添加所以我可以执行我的逻辑。

Is there any way to determine when the items have been added so I can perform my logic.

Cheers
Abs

Cheers Abs

推荐答案

显然,您看到的行为意味着实体框架创建并填充类似于以下内容的集合:

Apparently the behaviour you are seeing means that Entity Framework creates and fills the collection similar to this:

// setter called with empty collection
dbAppointment.ExceptionOcurrences = new HashSet<DbExceptionOcurrence>();

// only getter gets called now
dbAppointment.ExceptionOcurrences.Add(dbExceptionOcurrence1);
dbAppointment.ExceptionOcurrences.Add(dbExceptionOcurrence2);
dbAppointment.ExceptionOcurrences.Add(dbExceptionOcurrence3);
//...

我曾经希望你可以使用 ObjectMaterialized事件(可以使用 DbContext 如下例所示: https://stackoverflow.com/a/4765989/270591 ,EventArgs包含实体化实体),但不幸的是文档说:

I had hoped that you can use the ObjectMaterialized Event (can be registered with DbContext like in this example: https://stackoverflow.com/a/4765989/270591, the EventArgs contain the materialized entity) but unfortunately the documentation says:


此事件在所有标量,复杂和参考
属性已设置在一个对象上,但集合之前
加载

看起来,您必须在完整加载结果集合后运行结果集,并在导航集合中执行自定义逻辑的每个结果项目上调用一些方法。

It looks that you have to run through the result collection after it has been loaded completely and call some method on each result item which performs your custom logic on the navigation collection.

也许另一个选项是创建一个自定义使用添加方法的事件处理程序实现 ICollection< T> 的集合类型,并允许您钩住某些逻辑每次添加一个新项目。模型类中的导航集合必须是该类型的。也许甚至 ObservableCollection< T> 为此目的不错。

Maybe another option is create a custom collection type that implements ICollection<T> with an event handler for the Add method and allows you to hook in some logic everytime a new item is added. Your navigation collection in the model class would have to be of that type. Maybe even ObservableCollection<T> is fine for this purpose.

这篇关于EF 4.3(代码优先) - 确定项目何时添加到虚拟ICollection属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆