EF 6无法将数据从DGV提交到数据库

时间:2015-03-15 12:04:59

标签: vb.net linq entity-framework

我在从网格中将数据保存到数据库时遇到问题。我不知道我做错了所以请帮忙。这是我的示例代码。

代码:

Imports System.Linq
 Public Class Grid1

  Public Property Year As Integer
  Public Property User As Integer

  End Class

   Public Class Form1

   public db as new databaseContext

   Dim Query As List(Of Grid1) = (From c In db.table Select New Grid1 With _
                                                      {_ 
                                                       .Year = c.Year, _
                                                       .User= c.User _ 
                                                      }).ToList

   dgv.DataSource = Query 

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles   Button1.Click

       Try
            db.SaveChanges()   '  i don't have any exception it just don't commit changes!

       Catch ex As Exception
        MessageBox.Show(ex.InnerException.ToString, ex.Message)
       End Try

    End Sub

1 个答案:

答案 0 :(得分:0)

@Clasies是正确的,您不绑定任何连接到数据库的东西。您需要绑定到Entity实例。尝试类似:

dgv.DataSource = New ObservableCollection(Of table)(db.table.ToList())

编辑: 我添加了一个基本的sample project to GitHub和一个有效的EF6 DataGridView CRUD应用程序。