Vb.net 实体框架插入记录,不带“;加上;方法

Vb.net 实体框架插入记录,不带“;加上;方法,vb.net,entity-framework-4,crud,Vb.net,Entity Framework 4,Crud,我是EF的新手。我有一些代码可以成功地在表中插入记录。但是,它使用自动生成的“addto…”方法,据我所知,该方法已被折旧。我见过使用“add”方法的引用,但我遇到了麻烦。以下是有效的代码: Dim EntityContext As New DevEntities Dim log2 As New tblLog2 log2.Error = "This is a test." log2.Date = System.DateTime.Now EntityCon

我是EF的新手。我有一些代码可以成功地在表中插入记录。但是,它使用自动生成的“addto…”方法,据我所知,该方法已被折旧。我见过使用“add”方法的引用,但我遇到了麻烦。以下是有效的代码:

    Dim EntityContext As New DevEntities
    Dim log2 As New tblLog2

    log2.Error = "This is a test."
    log2.Date = System.DateTime.Now
    EntityContext.AddTotblLog2(log2)
    EntityContext.SaveChanges()
插入此记录的“正确”方法是什么(请用vb)?

是的,您需要使用:

    ...
    EntityContext.tblLog2s.AddObject(log2)
    EntityContext.SaveChanges()
基本上是围绕的包装器,因此上述调用相当于:

EntityContext.AddObject("tblLog2s", log2)    

注意:我假设
tblLog2s
tblLog2
实体的EntitySet名称。

如果您使用的是由设计器生成的类(而不是您自己的模板),您可以执行以下操作:

Dim EntityContext As New DevEntities
Dim log2 As New tblLog2

log2.Error = "This is a test."
log2.Date = System.DateTime.Now
EntityContext.tblLog2.AddObject(log2)
EntityContext.SaveChanges()
设计师还将为您创建以下方法,您可以使用这些方法:

AddTotblLog2(ByRef item As tblLog2)  
我在猜测那里的确切签名,但你明白了