我有以下代码(数据库是 SQL Server Compact 4.0):
Dim competitor=context.Competitors.Find(id)
当我对此进行分析时,Find 方法需要 300 多毫秒才能从仅包含 60 条记录的表中检索竞争对手。
当我将代码更改为:
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
CompetitionSubscriptions
我为和OpponentMeetings
使用 fluent API定义了多对多关系。
类的 ID 属性Competitor
是 Long ,由 Code First 转换为具有数据表中主键的 Identity 列(SQL Server Compact 4.0)
这里发生了什么??