我有以下代码(数据库是 SQL Server Compact 4.0):
Dim competitor=context.Competitors.Find(id)
当我进行性能分析时,从仅有60条记录的表中检索竞争对手的Find方法需要300多毫秒。
当我更改代码为:
Dim competitor=context.Competitors.SingleOrDefault(function(c) c.ID=id)
然后在仅3毫秒内找到了竞争者。
竞争者类:
Public Class Competitor
Implements IEquatable(Of Competitor)
Public Sub New()
CompetitionSubscriptions = New List(Of CompetitionSubscription)
OpponentMeetings = New List(Of Meeting)
GUID = GUID.NewGuid
End Sub
Public Sub New(name As String)
Me.New()
Me.Name = name
End Sub
'ID'
Public Property ID As Long
Public Property GUID As Guid
'NATIVE PROPERTIES'
Public Property Name As String
'NAVIGATION PROPERTIES'
Public Overridable Property CompetitionSubscriptions As ICollection(Of CompetitionSubscription)
Public Overridable Property OpponentMeetings As ICollection(Of Meeting)
End Class
我使用流畅的API为CompetitionSubscriptions
和OpponentMeetings
定义了多对多关系。
Competitor
类的ID属性是Long类型,Code First将其转换为Identity列,并作为主键存储在数据表中(SQL Server Compact 4.0)
这里发生了什么??