Entity Framework Association Performance
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 [d开发者_开发知识库bo].[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?
You are seeing the select statement used to retrieve the entity associated with this:
_mappedFoldersRepository.Query(m => m.FolderName == "Unassigned").FirstOrDefault();
The assignment to groupFile.MappedFolder
doesn't update the database until you call ObjectContext.SaveChanges
. At that point you should see the update statement.
精彩评论