Using SubmitChanges to create a LINQ Transaction?

The problem is, that the ID of your process is set when the changes are submitted to the database. Because you submit the changes after the line where you assign the process id to the product ( product. Process_Id = openProcess.Id ) The correct way to do this would be to correctly setup your database with a foreign key from PRODUCT to PROCESS and use the navigation property Process on Product to assign the process to the product The code would look like this: myDataContext db = new myDataContext(); Process openProcess = new Process(); openProcess.

Creation = DateTime. Now; openProcess. Number = pNumber; Product product = new Product(); product.

Code = pCode; product. Name = pName; product. Process = openProcess; db.Product.

InsertOnSubmit(product); db.SubmitChanges() Because Process is a navigation property of Product you don't need to insert the Process It will be inserted automatically, because you insert the "parent" - the Product.

The problem is, that the ID of your process is set when the changes are submitted to the database. Because you submit the changes after the line where you assign the process id to the product (product. Process_Id = openProcess.Id;).

The correct way to do this would be to correctly setup your database with a foreign key from PRODUCT to PROCESS and use the navigation property Process on Product to assign the process to the product. The code would look like this: myDataContext db = new myDataContext(); Process openProcess = new Process(); openProcess. Creation = DateTime.

Now; openProcess. Number = pNumber; Product product = new Product(); product. Code = pCode; product.

Name = pName; product. Process = openProcess; db.Product. InsertOnSubmit(product); db.SubmitChanges(); Because Process is a navigation property of Product, you don't need to insert the Process.

It will be inserted automatically, because you insert the "parent" - the Product.

Actually, the database is already designed like this. That's a really nice solution – Vitor Reis Sep 5 at 16:41.

You can make the whole thing transaciotnal using a TransactionScope e.g. Using (TransactionScope scope = new TransactionScope()) { myDataContext db = new myDataContext(); Process openProcess = new Process(); openProcess. Creation = DateTime. Now; openProcess.

Number = pNumber; db.Process. InsertOnSubmit(openProcess); db.SubmitChanges(); //openProcess.Id will be populated Product product = new Product(); product. Code = pCode; product.

Name = pName; product. Process_Id = openProcess. Id; db.Products.

InsertOnSubmit(product); // I assume you missed this step in your example db.SubmitChanges(); scope.Complete() } If an exception is thrown before scope.Complete() is called then the whole thing will be rolled back.

What's the differente betwen Products. Add(product) and Products. InsertOnSubtmit(product)?

– Vitor Reis Sep 8 at 15:01 @Vitor, That's a mistake I will correct it. – Ben Robinson Sep 8 at 15:06.

It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance. In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to create.

A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions