使用SqlCe和EntityFramework进行插入和更新的语法 [英] Syntax for Insert and Update with SqlCe and EntityFramework
问题描述
VS 2008, SqlCe 3.5
我正在尝试学习EntityFramework,但是无法获得基本的Insert和更新工作。当我包含SqlCe数据库(.sdf)时,向导会创建Test.edmx / designer.vb文件。从那里我创建我的数据文本,如下所示。表名是Users。
I'm trying to learn EntityFramework, but can't get the basic Insert and update to work. When I include the SqlCe database (.sdf) a wizard creates the Test.edmx/designer.vb file. From that I create my datacontext, like below. The table name is Users.
我的实体类的语法似乎与网络上的示例有所不同。这有点混乱,我不知道为什么会这样。下面我将展示两种不同的Insert方法,这两种方法在.SaveChanges行中都有异常:
The syntax of my entity classes seems a bit different from examples on the web. That's a bit confusing, and I don't know why this is. Below I show two different Insert methods, both of which gives exceptions on the .SaveChanges line:
An error occurred while updating the entries. See the InnerException for details.
{不支持服务器生成的密钥和服务器生成的值通过SQL Server Compact。}
{"Server-generated keys and server-generated values are not supported by SQL Server Compact."}
另外Update方法我真的不知道在缺少的部分写什么..会是非常高兴为这些问题提供一些帮助...
Also the Update method I have really no idea what to write in the missing part.. Would be very glad for some assistance on these issues...
Public Sub Insert(ByVal user As Users)
Dim ctx As New TestDBEntities1(connection)
ctx.Users.Context.AddObject("Users", user)
ctx.Users.Context.SaveChanges()
End Sub
Public Sub Insert(ByVal user As Users)
Dim ctx As New TestDBEntities1(connection)
ctx.AddToUsers(user)
ctx.SaveChanges()
End Sub
Public Sub Update(ByVal user As Users)
Dim ctx As New TestDBEntities1(connection)
Dim q = (From n In ctx.Users Where n.Id = user.Id Select n).Single
' How to update ??
ctx.SaveChanges()
End Sub
推荐答案
作为上面的评论;这是在SQL CE 4.0中修复的。
As comment above; This was fixed in SQL CE 4.0.
这篇关于使用SqlCe和EntityFramework进行插入和更新的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!