ef核心1.1.0相关实体async未插入

时间:2017-02-11 16:50:24

标签: c# entity-framework asynchronous entity-framework-core

EntityFramework核心1.1.0 async

var blog = new Blog
{
    Url = "http://blogs.msdn.com/dotnet",
    Posts = new List<Post>
    {
        new Post { Title = "Intro to C#" },
        new Post { Title = "Intro to VB.NET" },
        new Post { Title = "Intro to F#" }
    }
};

await _dbContext.Set<Blog>().AddAsync(blog);
await _dbContext.SaveChangesAsync();

生成的查询

INSERT INTO [Blogs] ([Url])
VALUES (@p0);
SELECT [BlogId]
FROM [Blogs]
WHERE @@ROWCOUNT = 1 AND [BlogId] = scope_identity();

使用_dbContext.SaveChangesAsync()时为什么不插入相关对象;

EntityFramework核心1.1.0同步

var blog = new Blog
{
Url = "http://blogs.msdn.com/dotnet",
Posts = new List<Post>
{
    new Post { Title = "Intro to C#" },
    new Post { Title = "Intro to VB.NET" },
    new Post { Title = "Intro to F#" }
}
};

_dbContext.Set<Blog>().Add(blog);
_dbContext.SaveChanges();

生成的查询

SET NOCOUNT ON;
INSERT INTO [Blogs] ([Url])
VALUES (@p0);
SELECT [BlogId]
FROM [Blogs]
WHERE @@ROWCOUNT = 1 AND [BlogId] = scope_identity();
Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory:Information: Executed DbCommand (4ms) [Parameters=[@p1='?', @p2='?' (Size = 4000), @p3='?' (Size = 4000), @p4='?', @p5='?' (Size = 4000), @p6='?' (Size = 4000), @p7='?', @p8='?' (Size = 4000), @p9='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
SET NOCOUNT ON;
DECLARE @toInsert0 TABLE ([BlogId] int, [Content] nvarchar(max), [Title] nvarchar(max), [_Position] [int]);
INSERT INTO @toInsert0
VALUES (@p1, @p2, @p3, 0),
(@p4, @p5, @p6, 1),
(@p7, @p8, @p9, 2);

DECLARE @inserted0 TABLE ([PostId] int, [_Position] [int]);
MERGE [Posts] USING @toInsert0 AS i ON 1=0
WHEN NOT MATCHED THEN
INSERT ([BlogId], [Content], [Title])
VALUES (i.[BlogId], i.[Content], i.[Title])
OUTPUT INSERTED.[PostId], i._Position
INTO @inserted0;

SELECT [t].[PostId] FROM [Posts] t
INNER JOIN @inserted0 i ON ([t].[PostId] = [i].[PostId])
ORDER BY [i].[_Position];

我不明白为什么同步工作, async 无法与相关实体一起使用。

它是entityframework核心1.1.0中的错误吗? 任何配置?

1 个答案:

答案 0 :(得分:-1)

抱歉,但我为工作做了改变

更改

await _dbContext.Set<Blog>().AddAsync(blog);

_dbContext.Set<Blog>().Add(blog);