我正在尝试使用 Entity Framework 连接 MySQL 数据库。我的 VB.net 代码如下:
Dim context As sql688249Entities = New sql688249Entities()
Dim nsv As sinhvien = New sinhvien()
nsv.MSSV = Int(234)
nsv.Hoten = "Thanh"
nsv.Diachi = "Phu Yen"
context.sinhviens.AddObject(nsv)
context.SaveChanges()
DataGridView1.DataSource = context.sinhviens
但它不起作用。如果我删除 context.Savechanges()
它没有问题。我不明白为什么我不能将记录插入数据库。
最佳答案
您尝试添加的类具有用于构建记录的Create...
方法。确保您也增加了表的主键字段。
Using context As sql688249Entities = New sql688249Entities()
Dim id As Integer = 1 ' get the last id or use this default
Dim lastOne = context.sinhviens.OrderByDescending(Function(s) s s.MSSV).FirstOrDefault
If lastOne IsNot Nothing Then
id = lastOne.MSSV + 1 ' add one to have a valid next id
End If
Dim nsv As sinhvien = sinhvien.Createsinhvien(id, "Thanh", "Phu Yen")
context.sinhviens.AddObject(nsv)
DataGridView1.DataSource = context.sinhviens
context.SaveChanges()
End Using
关于mysql - Visual Studio 2010 + MySQL + Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32366197/