C# 实体框架在调用INSERT之前加载所有行
我有一些实体框架代码,我认为这不是很有效。基本上,我有一个ApplicationUser类,如下所示:C# 实体框架在调用INSERT之前加载所有行,c#,.net,entity-framework,C#,.net,Entity Framework,我有一些实体框架代码,我认为这不是很有效。基本上,我有一个ApplicationUser类,如下所示: public class ApplicationUser : IdentityUser { public string CompanyName { get; set; } public virtual ICollection<SolverRun> Runs { get; set; } // .. other stuff generated by the Visu
public class ApplicationUser : IdentityUser
{
public string CompanyName { get; set; }
public virtual ICollection<SolverRun> Runs { get; set; }
// .. other stuff generated by the Visual Studio template
}
这很有效。但是,我看到的是以下查询:
exec sp_executesql N'SELECT
[Extent1].[SolverRunID] AS [SolverRunID],
[Extent1].[RunName] AS [RunName],
[Extent1].[Base] AS [Base],
[Extent1].[Start] AS [Start],
[Extent1].[Duration] AS [Duration],
[Extent1].[Result] AS [Result],
[Extent1].[ApplicationUser_Id] AS [ApplicationUser_Id]
FROM [dbo].[SolverRuns] AS [Extent1]
WHERE ([Extent1].[ApplicationUser_Id] IS NOT NULL) AND ([Extent1].[ApplicationUser_Id] = @EntityKeyValue1)',N'@EntityKeyValue1 nvarchar(128)',@EntityKeyValue1=N'029a20a9-b487-4579-87d0-50608ed7f058'
它将为该用户选择SolverRuns
表中的每一行。我怀疑currentUser.Runs
的getter上发生了这种情况
我担心随着用户增加越来越多的跑步次数(最终可能会有数千次),速度会越来越慢。有没有办法只添加新行,而不先选择所有内容?谢谢
更新:
根据py3r3str下面的回答,以下是我的新工作代码(在更改模型、迁移数据库以及其他之后):
请注意,调用
currentUser.Runs
将按照预期延迟加载所有运行。最简单的方法之一是将外键添加到您的SolverRun
实体:
class SolverRun
{
...
public string ApplicationUserId { get; set; }
[ForeignKey("ApplicationUserId")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
并使用ApplicationContext仅保存SolverRun
实体:
var solverRun = new SolverRun
{
Base = job.Base,
Duration = context.JobRunTime,
Result = (context.Result ?? "").ToString(),
RunName = job.RunName,
Start = context.FireTimeUtc.GetValueOrDefault().LocalDateTime,
ApplicationUserId = OwnerId
};
dbContext.Set<SolverRun>().Add(solverRun);
dbContext.SaveChanges();
var solverRun=新的solverRun
{
Base=job.Base,
Duration=context.JobRunTime,
结果=(context.Result??).ToString(),
RunName=job.RunName,
Start=context.FireTimeUtc.GetValueOrDefault().LocalDateTime,
applicationSerid=OwnerId
};
dbContext.Set().Add(solverRun);
dbContext.SaveChanges();
在这种情况下,EF负责在
ApplicationUser.Runs
中添加对象,以便只更新您可以看到的属性子集,但这些属性似乎不起作用。我得到一个异常:{“一个实体对象不能被多个IEntityChangeTracker实例引用。”}System.exception{System.InvalidOperationException}
注意,我将您的代码applicationserid=OwnerId
更改为ApplicationUser=currentUser
,因为显然您不能将字符串设置为ApplicationUser
无需担心,我是个白痴。我两次更新了一个ApplicationDbContext
。你的答案很完美。我很高兴能帮上忙;)我已经用最终的工作代码更新了我的问题。
exec sp_executesql N'INSERT [dbo].[SolverRuns]([ApplicationUserId], [RunName], [Base], [Start], [Duration], [Result])
VALUES (@0, @1, @2, @3, @4, @5)
SELECT [SolverRunID]
FROM [dbo].[SolverRuns]
WHERE @@ROWCOUNT > 0 AND [SolverRunID] = scope_identity()',N'@0 nvarchar(128),@1 nvarchar(max) ,@2 nvarchar(max) ,@3 datetime2(7),@4 time(7),@5 nvarchar(max) ',@0=N'029a20a9-b487-4579-87d0-50608ed7f058',@1=N'PilotRun',@2=N'YUL',@3='2015-09-03 11:07:32.0353181',@4='00:00:04.1521401',@5=N'Success'
class SolverRun
{
...
public string ApplicationUserId { get; set; }
[ForeignKey("ApplicationUserId")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
var solverRun = new SolverRun
{
Base = job.Base,
Duration = context.JobRunTime,
Result = (context.Result ?? "").ToString(),
RunName = job.RunName,
Start = context.FireTimeUtc.GetValueOrDefault().LocalDateTime,
ApplicationUserId = OwnerId
};
dbContext.Set<SolverRun>().Add(solverRun);
dbContext.SaveChanges();