2015-10-14 56 views
3

我有EF7一个对象模型看起来是这样的:数据在EF7保存带有一个对象 - 与许多

Story -> StoryPhotos[] 

我建立模型,通过一个表单提交一个故事,其中用户可以的还附上N张照片。模型创建看起来像这样:

var story = new Story 
{ 
    // ... 

    StoryPhotos = myFormData.Photos.Select(p => new StoryPhoto { 
    Name = p.Name, 
    FileName = p.FileName, 
    etc. 
    }).ToList() 
} 

然后,一旦我建立了我的数据模型,我通过存储库保存数据。

await repository.AddOrUpdateStoryAsync(story) 

它看起来像这样

if (story.Id == 0) 
{ 
    context.Stories.Add(story); 
    context.StoryPhotos.AddRange(story.StoryPhotos); 
} 
else 
{ 
    // Update properties for an edit. 
} 
await context.SaveChangesAsync(); 

这个伟大的工程......只要我只有一个StoryPhoto。当我把一个以上的到阵列中,我得到这个异常:

An unhandled exception occurred while processing the request. 

SqlException: Incorrect syntax near ','. 
System.Data.SqlClient.SqlCommand.<>c__DisplayClass16.<ExecuteDbDataReaderAsync>b__17(Task`1 result) 

我的研究表明,已经出现了一些不同的问题,有关于EF7是如何保存的对象图,但我在迷茫这种情况下,一个人可以节省罚款,多于一个则不会,并且不会对代码进行其他更改。

我正在使用EntityFramework-SqlServer 7.0.0-beta7。

+0

当处理ORM时,SQL语法异常肯定是一个错误 – haim770

+0

@ haim770我想知道(尤其是考虑测试版状态)。但是,当然,历史告诉我,我不应该首先责怪图书馆。我将在明天在Github上查看源代码作为另一个途径。如果有人能给我另一个想法,我会留下问题。 – JasCav

+0

您的代码似乎没有任何问题。现在EF 7 beta 8已经发布,您可以尝试升级到该版本,以查看是否可以解决问题。 – htuomola

回答

0

那么,这里是答案。我还发布了它在response to the issue(这不是问题)在ASP.NET 5实体框架GitHub上进行跟踪。任何遇到它的人都可以在这里重复。

好的,我可以直接在我的SQL数据库上重新创建问题。这是正在生成的查询。

INSERT INTO [StoryPhoto] ([FilePath], [StoryId]) 
     OUTPUT INSERTED.[Id] 
     VALUES ('path1', '174'), 
     ('path2', '174'), 
     ('path3', '174'); 

而且我得到这个错误:

Msg 102, Level 15, State 1, Line 3 
Incorrect syntax near ','. 

此外,能够找到这一点,我Google了我原来的问题有点不同(当我第一次碰到这种跑法),结果this post on SO,其中包括这个答案:

In order to use the multi-row VALUES(),() syntax, you need to be running SQL Server 2008 (or newer).

Since you are running SQL Server 2005, you need to run separate insert statements, use UNION/UNION ALL, or upgrade the instance (which is separate from Management Studio, which is just a client tool you use to connect to instances running any number of versions of SQL Server).

所以,这是我原来的帖子中的怀疑。这个特定的应用程序仍在运行SQL Server 2005.

这使我找到了EntityFramework.MicrosoftSqlServer支持的数据库版本,它是2008年和更高版本。

相关问题