如何观察DbSet 1所述的添加操作; T>? [英] How to observe the Add action of DbSet<T>?

查看:109
本文介绍了如何观察DbSet 1所述的添加操作; T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为联系方式 ContactField 为以下两类。当 ContactField 中加入联系方式,我希望能够分配 SortOrder的 ContactField 。我是否需要继承DbSet和定制添加的方法?如何实现的呢?

 公共类Foo {
        私人MyDbContext _db =新MyDbContext();

        公共无效的HelloWorld(){
            联系方式联系方式= ....; //<从数据库中的联系人。

            ContactField场= ....; ///<新领域
            ... ///<分配其他的属性到这个`field`
            field.FieldType = FieldType.Phone;

            //如何自动更新'SortOrder`
            //添加场到`ContactFields`时
            contact.ContactFields.Add(场);

            _db.SaveChanges();
        }
}

公共类联系{
        众长的ContactID {获得;组; }

        公共字符串显示名称{获得;组; }
        公共字符串DisplayCompany {获得;组; }
        公开日期时间CreatedTime {获得;组; }
        公开日期时间ModifiedTime {获得;组; }

        //原来的codeS
        //公共虚拟的ICollection< ContactField> ContactFields {获得;组; }
        公共虚拟MYLIST< ContactField> ContactFields {获得;组; }
}

 公共类ContactField {
        众长ContactFieldID {获得;组; }
        公众诠释SortOrder的{获得;组; }
        公众诠释的FieldType {获得;组; }

        公共字符串值{获得;组; }
        公共字符串标签{获得;组; }

        [专栏(的ContactID)
        公众诠释的ContactID {获得;组; }
        公共虚拟联络联系{获得;组; }
 }
 

编辑:     我发现我需要的是监督的变化的ICollection< ContactField> ContactFields 。而名单,其中,T> 的ICollection&LT的实现; T> 。所以,我创建一个自定义的 MYLIST ,并要求其通知 MYLIST 容器的变化。我将测试它的工作原理或不晚。

 公共类MYLIST< TEntity> :列表< TEntity> {
        公共委托OnAddHandler(对象发件人,TEntity项);
        公共事件OnAddHandler OnAddEvent;

        大众新空加(TEntity实体){
             OnAddEvent(这一点,实体);
             base.Add(实体);
        }
 }
 

解决方案

该DbSet拥有的本地属性,它是一个的ObservableCollection 。您可以订阅 Col​​lectionChanged 事件和更新排序顺序出现。

 公共类Foo {
        私人MyDbContext _db =新MyDbContext();

        公共无效的HelloWorld(){

            _db.Contacts.Local.CollectionChanged + = ContactsChanged;

            联系方式联系方式= ....; //<从数据库中的联系人。

            ContactField场= ....; ///<新领域
            ... ///<分配其他的属性到这个`field`
            field.FieldType = FieldType.Phone;

            //如何自动更新'SortOrder`
            //添加场到`ContactFields`时
            contact.ContactFields.Add(场);

            _db.SaveChanges();
        }

        公共无效ContactsChanged(对象发件人,NotifyCollectionChangedEventArgs参数){

            如果(args.Action == NotifyCollectionChangedAction.Add)
            {

                // 分类
            }
        }
}
 

I has two classes named Contact and ContactField as following. When the ContactField is added into Contact, I hope to assign SortOrder to ContactField automatically. Do I need to inherit DbSet and customize the Add method ? How to achieve it ?

public class Foo {
        private MyDbContext _db = new MyDbContext();

        public void HelloWorld() {
            Contact contact = ....; //< A contact from database.

            ContactField field = ....; ///< A new field 
            .... ///< assign other properties into this `field`
            field.FieldType = FieldType.Phone;

            // How to automatically update `SortOrder` 
            // when adding field into `ContactFields`
            contact.ContactFields.Add(field);

            _db.SaveChanges();
        }
}

public class Contact {
        public long ContactID { get; set; }

        public string DisplayName { get; set; }
        public string DisplayCompany { get; set; }
        public DateTime CreatedTime { get; set; }
        public DateTime ModifiedTime { get; set; }

        // Original codes    
        //public virtual ICollection<ContactField> ContactFields { get; set; }
        public virtual MyList<ContactField> ContactFields { get; set; }
}

 public class ContactField {
        public long ContactFieldID { get; set; }
        public int SortOrder { get; set; }
        public int FieldType { get; set; }

        public string Value { get; set; }
        public string Label { get; set; }

        [Column("ContactID")]
        public int ContactID { get; set; }
        public virtual Contact Contact { get; set; }
 }

Edit: I found what I need is to monitor the changes of ICollection<ContactField> ContactFields. And the List<T> is an implementation of ICollection<T>. So, I create a custom MyList and ask it notifies the changes of MyList container. I will test it works or not later.

public class MyList<TEntity> : List<TEntity> {
        public delegate OnAddHandler(object sender, TEntity entry);
        public event OnAddHandler OnAddEvent;

        public new void Add(TEntity entity) {
             OnAddEvent(this, entity); 
             base.Add(entity);
        }
 }

解决方案

The DbSet has a Local property which is an ObservableCollection. You can subscribe to the CollectionChanged event and update the sort order there.

public class Foo {
        private MyDbContext _db = new MyDbContext();

        public void HelloWorld() {

            _db.Contacts.Local.CollectionChanged += ContactsChanged;

            Contact contact = ....; //< A contact from database.

            ContactField field = ....; ///< A new field 
            .... ///< assign other properties into this `field`
            field.FieldType = FieldType.Phone;

            // How to automatically update `SortOrder` 
            // when adding field into `ContactFields`
            contact.ContactFields.Add(field);

            _db.SaveChanges();
        }

        public void ContactsChanged(object sender, NotifyCollectionChangedEventArgs args) {

            if (args.Action == NotifyCollectionChangedAction.Add)
            {

                // sort
            }    
        }
}

这篇关于如何观察DbSet 1所述的添加操作; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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