简体   繁体   English

LINQ 2 SQL:AddObject和InsertOnSubmit

[英]LINQ 2 SQL: AddObject and InsertOnSubmit

I need to insert a record into Payment table. 我需要在付款表中插入一条记录。 I found two methods to do it 我发现了两种方法

  1. AddObject ADDOBJECT
  2. InsertOnSubmit 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 . 它们的作用大致相同,但是AddObject属于Microsoft的Entity FrameworkInsertOnSubmit属于Microsoft的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. 在AddObject之后调用SaveChanges ,在InsertOnSubmit之后调用SubmitChanges

Since you mentioned that you're using LINQ to SQL, you probably want InsertOnSubmit. 由于您提到要使用LINQ to SQL,因此您可能希望使用InsertOnSubmit。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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