如何观察DbSet 1所述的添加操作; T&GT;? [英] How to observe the Add action of DbSet<T>?
问题描述
我有一个名为联系方式
和 ContactField
为以下两类。当 ContactField
中加入联系方式
,我希望能够分配 SortOrder的
到 ContactField
。我是否需要继承DbSet和定制添加
的方法?如何实现的呢?
公共类Foo {
私人MyDbContext _db =新MyDbContext();
公共无效的HelloWorld(){
联系方式联系方式= ....; //&LT;从数据库中的联系人。
ContactField场= ....; ///&LT;新领域
... ///&LT;分配其他的属性到这个`field`
field.FieldType = FieldType.Phone;
//如何自动更新'SortOrder`
//添加场到`ContactFields`时
contact.ContactFields.Add(场);
_db.SaveChanges();
}
}
公共类联系{
众长的ContactID {获得;组; }
公共字符串显示名称{获得;组; }
公共字符串DisplayCompany {获得;组; }
公开日期时间CreatedTime {获得;组; }
公开日期时间ModifiedTime {获得;组; }
//原来的codeS
//公共虚拟的ICollection&LT; ContactField&GT; ContactFields {获得;组; }
公共虚拟MYLIST&LT; ContactField&GT; ContactFields {获得;组; }
}
公共类ContactField {
众长ContactFieldID {获得;组; }
公众诠释SortOrder的{获得;组; }
公众诠释的FieldType {获得;组; }
公共字符串值{获得;组; }
公共字符串标签{获得;组; }
[专栏(的ContactID)
公众诠释的ContactID {获得;组; }
公共虚拟联络联系{获得;组; }
}
编辑:
我发现我需要的是监督的变化的ICollection&LT; ContactField&GT; ContactFields
。而名单,其中,T&GT;
是的ICollection&LT的实现; T&GT;
。所以,我创建一个自定义的 MYLIST
,并要求其通知 MYLIST
容器的变化。我将测试它的工作原理或不晚。
公共类MYLIST&LT; TEntity&GT; :列表&LT; TEntity&GT; {
公共委托OnAddHandler(对象发件人,TEntity项);
公共事件OnAddHandler OnAddEvent;
大众新空加(TEntity实体){
OnAddEvent(这一点,实体);
base.Add(实体);
}
}
该DbSet拥有的本地属性,它是一个的ObservableCollection
。您可以订阅 CollectionChanged
事件和更新排序顺序出现。
公共类Foo {
私人MyDbContext _db =新MyDbContext();
公共无效的HelloWorld(){
_db.Contacts.Local.CollectionChanged + = ContactsChanged;
联系方式联系方式= ....; //&LT;从数据库中的联系人。
ContactField场= ....; ///&LT;新领域
... ///&LT;分配其他的属性到这个`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屋!