Văn Hữu
Văn Hữu

Reputation: 164

LinQ to SQL - InsertOnSubmit without SubmitChanges

Suppose that I inserted an entity to an entity set by calling the InsertOnSubmit method , like this :

DBDataContext db = new DBDataContext();
Product product = new Product {ID = "1" , Name = "Chair"};
db.Products.InsertOnSubmit(product);
// db.SubmitChanges();

I didnt call SubmitChanges intentionally . My question is , can i later find that product and delete it with DeleteOnSubmit method?:

Product product = db.Products.Single(p => p.ID == "1");
db.Products.DeleteOnSubmit(product);

If i cannot , then what the InsertOnSubmit method actually does?

Thank you in advance for your help.

Upvotes: 1

Views: 265

Answers (1)

sgmoore
sgmoore

Reputation: 16067

The InsertOnSubmit method adds the entity to a list of entities which will be inserted when you call SubmitChanges.

My question is , can i later find that product and delete it with DeleteOnSubmit method?:

You won't be able to find the entity because it does not exist in the database.

Note, if you leave out the find, ie if your code was

DBDataContext db = new DBDataContext();
Product product = new Product {ID = "1" , Name = "Chair"};
db.Products.InsertOnSubmit(product);
db.Products.DeleteOnSubmit(product);

Then this would work as it wouldn't go near the database. The DeleteOnSubmit command would remove `product' from the list of entities to be inserted when you call SubmitChanges.

Upvotes: 1

Related Questions