繁体   English   中英

如何观察DbSet的Add动作 <T> ?

[英]How to observe the Add action of DbSet<T>?

我有两个名为ContactContactField类,如下所示。 ContactField添加到Contact ,我希望自动将SortOrder分配给ContactField 我是否需要继承DbSet并自定义Add方法? 怎么实现呢?

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

编辑:我发现我需要的是监视ICollection<ContactField> ContactFields的变化。 List<T>ICollection<T> 因此,我创建了一个自定义MyList并要求它通知MyList容器的更改。 我会测试它是否有效。

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

DbSet有一个Local属性,它是一个ObservableCollection 您可以订阅CollectionChanged事件并更新其中的排序顺序。

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

或者,重写DbContext上的SaveChanges方法,并使用本地ChangeTracker属性查找特定类型的新实体并设置其排序顺序属性。 适用于将上次更新日期等内容设置为1个位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM