c# - 如何观察DbSet<T>的Add Action ?

标签 c# .net entity-framework entity-framework-4.1 ef-code-first

我有两个名为 Contact 的类和 ContactField如下。当 ContactField添加到 Contact , 希望赋值SortOrderContactField自动地。我是否需要继承 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
            }    
        }
}

关于c# - 如何观察DbSet<T>的Add Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9543208/

相关文章:

c# - 如何使用 JustMock 模拟 EF 6 异步方法?

c# - xs :key, 为什么当键值不是键引用的成员时验证通过?

database - Entity Framework 5 迁移中不可能进行数据库重构吗?

c# - 在什么情况下,C#中的赋值不能更改变量的值?

c# - 将列表类型转换为 IEnumerable 接口(interface)类型

C# 更新 MySql 数据库不工作

c# - 重新部署旧版本应用程序数据库的策略

c# - 并行运行测试时为 WebApi OWIN 自主机获取免费端口

.net - 如何在 .NET 中显示带上划线的字母?

entity-framework - 分离 Entity Framework poco 和 objectcontext