简体   繁体   中英

LINQ 2 SQL: AddObject and InsertOnSubmit

I need to insert a record into Payment table. I found two methods to do it

  1. AddObject
  2. InsertOnSubmit

What is the difference between the two? When to use what?

    public void InsertEntity(DBML_Project.Payment payment)
    {
        //Insert the entity
        MyDataContext.GetTable<DBML_Project.Payment>().InsertOnSubmit(payment);
    }


public void InsertPayment(IPayment payment)
{
     this.AddObject(payment.GetType().Name, payment);
}

Here are some related questions:

They do roughly the same thing, but AddObject belongs to Microsoft's Entity Framework and InsertOnSubmit belongs to Microsoft's LINQ to SQL .

For either of them, you will want to commit the changes to the database when you're done adding objects. Call SaveChanges after AddObject and call SubmitChanges after InsertOnSubmit.

Since you mentioned that you're using LINQ to SQL, you probably want InsertOnSubmit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM