基本上,我在一个事务中插入35000个对象:
using(var uow = new MyContext()){
for(int i = 1; i < 35000; i++) {
var o = new MyObject()...;
uow.MySet.Add(o);
}
uow.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
这需要永远!如果我使用底层ObjectContext(通过使用IObjectAdapter),它仍然很慢但需要大约20秒.它似乎DbSet<>正在进行一些线性搜索,这需要花费大量时间......
其他人看到这个问题?
我有以下代码(数据库是SQL Server Compact 4.0):
Dim competitor=context.Competitors.Find(id)
Run Code Online (Sandbox Code Playgroud)
当我对此进行分析时,Find方法需要300 + ms才能从仅有60条记录的表中检索竞争对手.
当我将代码更改为:
Dim competitor=context.Competitors.SingleOrDefault(function(c) c.ID=id)
Run Code Online (Sandbox Code Playgroud)
然后在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 …Run Code Online (Sandbox Code Playgroud) entity-framework ef-code-first sql-server-ce-4 dbcontext dbset
我们的代码中有一个通用的更新方法
foreach (var entity in entityList)
{
Context.GetIDbSet<T>().Attach(entity);
Context.SetState(entity, EntityState.Modified);
}
Run Code Online (Sandbox Code Playgroud)
我通过传入一个实体的枚举并按每个实体调用一次来测试它.
我发现的是1000个实体的枚举大约需要47秒才能运行.这是预期的行为吗?或者代码片段有问题吗?
分析显示Attach()方法比SetState()方法慢.
我运行它的测试是在一个有50个属性的实体上,如果有任何影响则没有关系.