实体框架关联性能

发布于 2024-10-12 23:58:31 字数 2364 浏览 0 评论 0原文

我有以下关联.. GroupFile 与 MappedFolders 具有一对多关系(通过 MappedFolder 导航属性)。

alt text

我希望下一行进行更新:

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).

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2024-10-19 23:58:31

您将看到用于检索与此关联的实体的 select 语句:

_mappedFoldersRepository.Query(m => m.FolderName == "Unassigned").FirstOrDefault();

在调用 ObjectContext.SaveChanges 之前,对 groupFile.MappedFolder 的分配不会更新数据库。此时您应该会看到更新语句。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文