I'm new to EF. I have some code that successfully inserts a record in a table. However, it uses the autogenerated "addto..." method which I understa开发者_高级运维nd is depreciated. I've seen references to using the "add" method but am having trouble. Here is the code that works:
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()
What is the "correct" way to insert this record (vb please)?
Yes, you need to use ObjectSet<TEntity>.AddObject Method:
...
EntityContext.tblLog2s.AddObject(log2)
EntityContext.SaveChanges()
Basically ObjectSet.AddObject is a wrapper around the ObjectContext.AddObject method so the above call is equivalent to :
EntityContext.AddObject("tblLog2s", log2)
Note: I assume that tblLog2s
is the EntitySet name oftblLog2
entity.
If you're using the classes that are generated by the designer (rather than your own templates), you would do:
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()
The designer would also have created the following method for you, which you could use:
AddTotblLog2(ByRef item As tblLog2)
I'm guessing on the exact signature there, but you get the idea.
精彩评论