实体框架关联性能
我有以下关联.. GroupFile 与 MappedFolders 具有一对多关系(通过 MappedFolder 导航属性)。
我希望下一行进行更新:
groupFile.MappedFolder = _mappedFoldersRepository.Query(m => m.FolderName == "Unassigned").FirstOrDefault();
查询(根据探查器)正在执行以下 SQL:
SELECT TOP ( 1 ) [Extent1].[MappedFolderId] AS [MappedFolderId],
[Extent1].[FolderPath] AS [FolderPath],
[Extent1].[FolderName] AS [FolderName],
[Extent1].[HotFolder] AS [HotFolder],
[Extent1].[Workstation] AS [Workstation]
FROM [dbo].[MappedFolders] AS [Extent1]
WHERE N'Unassigned' = [Extent1].[FolderName]
但根据 Entity Framework Profiler,分配本身在开始事务之前执行此 SQL 语句:
SELECT [Extent1].[Id] AS [Id],
[Extent1].[Path] AS [Path],
[Extent1].[Status] AS [Status],
[Extent1].[DateAdded] AS [DateAdded],
[Extent1].[DateLastUpdated] AS [DateLastUpdated],
[Extent1].[JobSetup_SetupId] AS [JobSetup_SetupId],
[Extent1].[Group_GroupId] AS [Group_GroupId],
[Extent1].[MappedFolder_MappedFolderId] AS [MappedFolder_MappedFolderId]
FROM [dbo].[GroupFiles] AS [Extent1]
WHERE [Extent1].[MappedFolder_MappedFolderId] = 7 /* @EntityKeyValue1 */
我一定错过了一些微妙的(或不那么微妙)的东西,但我不确定为什么需要该 SQL 语句...... 更新: 进一步查看探查器告诉我的内容,SQL 语句与以下内容相关联:
public virtual MappedFolders MappedFolder
{
get { return _mappedFolder; }
set
{
if (!ReferenceEquals(_mappedFolder, value))
{
var previousValue = _mappedFolder;
_mappedFolder = value;
FixupMappedFolder(previousValue);
}
}
}
private void FixupMappedFolder(MappedFolders previousValue)
{
if (previousValue != null && previousValue.GroupFiles.Contains(this))
{
previousValue.GroupFiles.Remove(this);
}
if (MappedFolder != null)
{
// THIS IS WHAT THE SQL STATEMENT IS FOR!!!!!
if (!MappedFolder.GroupFiles.Contains(this))
{
MappedFolder.GroupFiles.Add(this);
}
}
}
生成的代码正在检查映射文件夹的 GroupFiles 集合以查看它是否已添加?
I have the following association.. GroupFile has a one to many relationship with MappedFolders (via the MappedFolder navigation property).
I would expect the next line to do an update:
groupFile.MappedFolder = _mappedFoldersRepository.Query(m => m.FolderName == "Unassigned").FirstOrDefault();
The Query (according to the profiler) is executing this SQL:
SELECT TOP ( 1 ) [Extent1].[MappedFolderId] AS [MappedFolderId],
[Extent1].[FolderPath] AS [FolderPath],
[Extent1].[FolderName] AS [FolderName],
[Extent1].[HotFolder] AS [HotFolder],
[Extent1].[Workstation] AS [Workstation]
FROM [dbo].[MappedFolders] AS [Extent1]
WHERE N'Unassigned' = [Extent1].[FolderName]
But according to Entity Framework Profiler, the assignment itself is executing this SQL statement prior to beginning the transaction:
SELECT [Extent1].[Id] AS [Id],
[Extent1].[Path] AS [Path],
[Extent1].[Status] AS [Status],
[Extent1].[DateAdded] AS [DateAdded],
[Extent1].[DateLastUpdated] AS [DateLastUpdated],
[Extent1].[JobSetup_SetupId] AS [JobSetup_SetupId],
[Extent1].[Group_GroupId] AS [Group_GroupId],
[Extent1].[MappedFolder_MappedFolderId] AS [MappedFolder_MappedFolderId]
FROM [dbo].[GroupFiles] AS [Extent1]
WHERE [Extent1].[MappedFolder_MappedFolderId] = 7 /* @EntityKeyValue1 */
I must be missing something subtle (or not so subtle) but I'm not sure why that SQL statement is needed....
UPDATE:
Looking further into what the profiler is telling me, the SQL Statement is associated with the following:
public virtual MappedFolders MappedFolder
{
get { return _mappedFolder; }
set
{
if (!ReferenceEquals(_mappedFolder, value))
{
var previousValue = _mappedFolder;
_mappedFolder = value;
FixupMappedFolder(previousValue);
}
}
}
private void FixupMappedFolder(MappedFolders previousValue)
{
if (previousValue != null && previousValue.GroupFiles.Contains(this))
{
previousValue.GroupFiles.Remove(this);
}
if (MappedFolder != null)
{
// THIS IS WHAT THE SQL STATEMENT IS FOR!!!!!
if (!MappedFolder.GroupFiles.Contains(this))
{
MappedFolder.GroupFiles.Add(this);
}
}
}
The generated code is checking the GroupFiles collection of the mapped folder to see if it has already been added?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将看到用于检索与此关联的实体的 select 语句:
在调用
ObjectContext.SaveChanges
之前,对groupFile.MappedFolder
的分配不会更新数据库。此时您应该会看到更新语句。You are seeing the select statement used to retrieve the entity associated with this:
The assignment to
groupFile.MappedFolder
doesn't update the database until you callObjectContext.SaveChanges
. At that point you should see the update statement.