none
Insert new records in sql table ADO.NET Entity Framework and LINQ RRS feed

  • Question

  • I’m trying to insert a new record into a SQL table using ADO.NET Entity Framework and LINQ programmatically.

    Public Class CheckOutFrm

        Private dbcontext As New BookstoreDataClass.BookStoreEntities()

    Private Sub CreateAccountBtn_Click(sender As Object, e As EventArgs) Handles CreateAccountBtn.Click

            Dim newCustomer As New BookstoreDataClass.Customer

            With newCustomer

                .FirstName = FirstNameTxtBx.Text

                .LastName = LastNameTxtBx.Text

                .BillingName = BillNameTxtBx.Text

                .BillingAddress = BillAddressTxtBx.Text

                .BillingCity = BillCityTxtBx.Text

                .BillingState = BillStateComBx.Text

                .BillingZip = BillZipTxtBx.Text

                .ShippingName = ShipNameTxtBx.Text

                .ShippingAddress = ShipAddressTxtBx.Text

                .ShippingCity = ShipCityTxtBx.Text

                .ShippingState = ShipStateComBx.Text

                .ShippingZip = ShipZipTxtBx.Text

            End With

            dbcontext.AddToCustomers(newCustomer)

            dbcontext.Customers.Add(newCustomer)

           

            dbcontext.SaveChanges()

      End Sub

    The new record is not being created. I’ve been searching for a solution and all the examples I can find have an AddTo”TableName” method that I cannot find. Is there an Import I’m not using? I’ve only have the following ;

    Imports System.Linq

    Imports System.Data.Entity


    why767

    Monday, April 21, 2014 11:51 PM

Answers

  • I'm not using ;

    dbcontext.AddToCustomers(newCustomer)

    and

    dbcontext.Customers.Add(newCustomer)

    The first one is for EF 5 and below.

    The second one is for EF6.

    Maybe you need to place a try/catch around the code to possible catch any exception being thrown. 

    • Marked as answer by why767 Friday, April 25, 2014 12:53 PM
    Tuesday, April 22, 2014 2:54 AM

All replies

  • db.AddToAuthor(auth);

    db.SaveChanges();

    No matter if it's VB or C#, the above two methods are what you need to save an object to database using EF.

    The table where the object is to be saved/added/inserted must be a table that using an Identity column as the primary-key.

    If the primary-key property in the EF entity/object is = 0, then EF will add the object. If the primary-key property in the EF entity/object, the object will be updated on the database table .

    Take note: When you are persisting data to the database, you are not using Linq, and you are using Object Oriented Programming.

    dbcontext is an object that has methods (behavior). The methods (behaviors) of the dbcontext you are trying to use are dbcontext.AddToCustomers() dbcontext.SaveChanges(). Those behaviors of dbcontext have nothing to do with Linq.

    Linq is a query language that allows the database, collection, array or etc etc to be queried.

    Tuesday, April 22, 2014 1:51 AM
  • I'm not using ;

    dbcontext.AddToCustomers(newCustomer)

    and

    dbcontext.Customers.Add(newCustomer)

    at the same time. I get "method does not exist" error with the dbcontext.AddToCustomers(newCustomer) command and intellisense does not have it listed.

    Thanks for your reply


    why767

    Tuesday, April 22, 2014 2:27 AM
  • I'm not using ;

    dbcontext.AddToCustomers(newCustomer)

    and

    dbcontext.Customers.Add(newCustomer)

    The first one is for EF 5 and below.

    The second one is for EF6.

    Maybe you need to place a try/catch around the code to possible catch any exception being thrown. 

    • Marked as answer by why767 Friday, April 25, 2014 12:53 PM
    Tuesday, April 22, 2014 2:54 AM
  • I found that I had two thing that confused me in my project. First was the file containing the database was in two locations in the project. The second was every time the project is compiled all changes to the database was being re-written deleting the changes or add records.

    Thanks for your help


    why767

    Friday, April 25, 2014 12:52 PM