Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityFramework生成的奇怪SQL查询_C#_Asp.net_Sql_Sql Server_Entity Framework - Fatal编程技术网

C# EntityFramework生成的奇怪SQL查询

C# EntityFramework生成的奇怪SQL查询,c#,asp.net,sql,sql-server,entity-framework,C#,Asp.net,Sql,Sql Server,Entity Framework,这是我的SQL查询,它是由EF生成的 The thread '<No Name>' (0x1884) has exited with code 0 (0x0). Opened connection at 26-Mar-14 23:39:33 +01:00 Opened connection at 26-Mar-14 23:39:35 +01:00 Started transaction at 26-Mar-14 23:39:35 +01:00 Started transaction

这是我的SQL查询,它是由EF生成的

The thread '<No Name>' (0x1884) has exited with code 0 (0x0).
Opened connection at 26-Mar-14 23:39:33 +01:00
Opened connection at 26-Mar-14 23:39:35 +01:00
Started transaction at 26-Mar-14 23:39:35 +01:00
Started transaction at 26-Mar-14 23:39:35 +01:00
INSERT [dbo].[Tagger]([tagCount], [imageId], [tag])
VALUES (@0, @1, @2)
SELECT [id]
FROM [dbo].[Tagger]
WHERE @@ROWCOUNT > 0 AND [id] = scope_identity()
-- @0: '1' (Type = Int32)
-- @1: '1' (Type = Int32)
-- @2: 'tag1' (Type = String, Size = -1)
-- Executing at 26-Mar-14 23:39:35 +01:00
INSERT [dbo].[Tagger]([tagCount], [imageId], [tag])
VALUES (@0, @1, @2)
SELECT [id]
FROM [dbo].[Tagger]
WHERE @@ROWCOUNT > 0 AND [id] = scope_identity()
-- @0: '1' (Type = Int32)
-- @1: '1' (Type = Int32)
-- Completed in 3 ms with result: SqlDataReader

-- @2: 'tag1' (Type = String, Size = -1)
-- Executing at 26-Mar-14 23:39:35 +01:00
-- Completed in 0 ms with result: SqlDataReader

Committed transaction at 26-Mar-14 23:39:35 +01:00
Committed transaction at 26-Mar-14 23:39:35 +01:00
Closed connection at 26-Mar-14 23:39:35 +01:00
Closed connection at 26-Mar-14 23:39:35 +01:00 
我觉得这很奇怪,因为我在控制器中的代码看起来像这样

private void addTagsToTagger(FormCollection form)
{

    char[] delimiter = { ',' };
    List<string> userTags = form["tags"].Split(delimiter).ToList();


    using (var ctx = new vestibulyContext())
    {
        ctx.Database.Log = s => log(s);
        foreach (var tagItem in userTags)
        {
            if (!IssetTagInDb(ctx, tagItem))
            {
                var item = tagItem.ToLower().Trim();
                var t = new Tagger() { imageId = Int16.Parse(form["imgId"]), tag = item, tagCount = 1 };
                ctx.Tagger.Add(t);
                ctx.SaveChanges();
            }
        }
    }

}

private bool IssetTagInDb(vestibulyContext ctx, string tag)
{
    tag = tag.ToLower().Trim();
    return ctx.Tagger.Any(t => t.tag == tag); 
}

所以,在foreach循环的每一步中,我都调用SaveChanges方法,我不知道为什么EF会生成这样的查询。开始时,有两条线路打开连接。为什么?在这2次之后启动事务,在这2次之后插入查询。这是正常的行为吗?我无法想象为什么在每一步中使用1次Add方法和1次SaveChanges方法时会有2次insert查询。因此,我的记录被2次添加到数据库中。知道这有什么问题吗?谢谢。

您是否使用断点来验证userTags中只有一项?addTagsToTagger只被调用一次?userTags只包含唯一的项,addTagsToTagger只被调用一次。