问题描述
我有两个类名为 Contact
和 ContactField
如下。当 ContactField
添加到联系人
中时,我希望分配 SortOrder
到 ContactField
自动。我需要继承DbSet并自定义添加
方法吗?如何实现?
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; }
}
编辑:
我发现我需要的是监视 ICollection< ContactField> ContactFields
。而列表< T>
是 ICollection< T>
的实现。所以,我创建一个自定义的 MyList
,并要求它通知 MyList
容器的更改。我将测试它的工作或不晚些。
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);
}
}
推荐答案
DbSet有一个属性,即 ObservableCollection
。您可以订阅 CollectionChanged
事件并更新排序顺序。
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的添加动作< T>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!