EF Code First/CollectionBase集合 [英] EF Code First / CollectionBase collections
问题描述
我有一个现有项目正在使用数据集作为存储机制.
我想先迁移到Entity Framework 4.1 Code.
我的类包含强类型的集合类,如下所示:
I have an existing project that is using data sets as a storing mechanism.
I want to migrate to Entity Framework 4.1 Code First.
My classes contain strongly typed collection classes, like this:
Public Class Calculations
Inherits CollectionBase
Public Event CalculationAdded(ByVal Calculation As Calculation)
#Region "Strongly Typed Collection Methods"
Default Public Overloads Property Item(ByVal index As Integer) As Calculation
Get
Return CType(Me.List.Item(index), Calculation)
End Get
Set(ByVal value As Calculation)
Me.List.Item(index) = value
End Set
End Property
Public Function Add(ByVal value As Calculation) As Integer
RaiseEvent CalculationAdded(value)
Return Me.List.Add(value)
End Function
Public Overloads Function Contains(ByVal obj As Calculation) As Boolean
Return Me.List.Contains(obj)
End Function
Public Overloads Function IndexOf(ByVal obj As Calculation) As Integer
Return Me.List.IndexOf(obj)
End Function
Public Overloads Sub Remove(ByVal obj As Calculation)
Me.RemoveAt(IndexOf(obj))
End Sub
Public Overloads Sub Sort()
Me.InnerList.Sort()
End Sub
#End Region
End Class
首先在代码示例代码中,每个人都使用List(Of T)存储某种类型的集合.但是我认为那样无法实现事件.
我在Google上搜索了很多,但是并没有带我去任何地方.任何建议都会有所帮助.
In the code examples of code first, everyone uses List(Of T) to store a collection of some type. But that way, I can''t implement events, I think.
I Googled a lot, but it doesn''t get me anywhere. Any advise would be helpful.
Can I use List(Of T) and still be able to fire events when an item is added to the collection?
推荐答案
嗨!
因此,您想使集合可见".您可以使用.NET 4.0中提供(可用)的其中一些(我认为它们在较早版本中不可用):
1. ObservableCollection< T> [ BindingList< T> [通用列表(C#) [ C5通用馆藏库 [ Powercollections [ ^ ]-不确定他们是否具有事件感知"集合?
或者您也可以这样编写自己的集合类型...
Hi!
So you want to make your collections "observable". You can use some of them provided (available) in .NET 4.0(I think that they are not available in earlier versions):
1. ObservableCollection<T>[^]
2. BindingList<T>[^] - please don''t use this for your purpose...
If you don''t have .NET 4.0 framework available I suggest you to use some community solution:
1. Generic List (C#)[^]
2. The C5 Generic Collection Library[^]
3. Powercollections[^] - Not sure that they have "event aware" collection?
Or you can write you own collection types like this...
public class YourList<T> : List<T>
{
public event System.EventHandler AddedItem;
public new void Add(T item)
{
base.Add(item);
if (AddedItem != null)
{
AddedItem(this, new EventArgs());
}
}
}
这只是存根,如果您想使用此示例,则需要处理 List 提供的所有其他方法...
这是另外一个示例 [
This is only stub and you need to handle all other methods that are provided by List if you want to use this example...
Here is one more example[^] if you decide to implement it by yourself.
这篇关于EF Code First/CollectionBase集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!