Google Search

Google
 

Monday, July 21, 2008

Video: Inserts with LINQ to SQL

Presenter:Mike Taulty

The title says it all - how do we do inserts when using LINQ to SQL?

The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video


Minute 0

Oh miners might halt it and I were from Microsoft in the UK this is one of a number of short videos about the new technology that is coming with visual studio codename augers that the next version of visual studio the technology is called in to sequel it puts the new language integrated clearing pattern on top of an object relational mapping API the latest obstacles is not a number of videos if you ve not seen the introductory video and data context video and the mapping video it may be worth starting with a lascivious price looking at this one is full of it is talk about inserting data and sequel server using them to sequel update service that I m using the March technology preview of visual studio codename augers that was the latest review at the time of making the video and add that a simple consul application is very similar to things with his two previous sessions in my consul application I ve gone and dragged in a couple of tables from the north and database to make a link to sequel diagram

Minute 1

Is a generously told from the and I was to discover that the London programme file here with some very and I went empty cold really other than the creating of data context it is going to allow us to interact with Norseman database and specifically the customers and orders table in gates at the first is as much as gone to assemble insert with increased something like an order 03 and will go to context of the orders entity set that we got there and we can add that order leaders created in a meeting course of the changes on the context and once we got to that point we you re a writer and old order ID which interestingly cars in Northland or driving is active against the column is generated by the database and yet that has it to do any work would you describe them like that thing have you ever gone on that code will see that we get an order back from the database was one of

Minute 2

And for that is the one who just inserted and the reason why we can go and do this with its order ID property that we got here it is merged with quit the wrong button allows that on click on a button is that the order ID property has been marked in the Meta data here as being database generated so that neither the framework know is that it is the only query that property having gone and done an insert on the database of the niceness of the framework of four as so it elsewhere in insert a customer and in order and a floodlit balustraded customer here is the back credit customer could see there was no think it is a customer diesel goods demo to families at the company name demo company and having created that we can create another order let s just go and walk up to the customer and into their orders collection

Minute 3

We will the new order that we create it and everything will go to the context and add to the customers the customer would have created we can submit changes and once again if Alice read at the order ID to the console will go and do that we get 11095 and if you are looking database the demo to we can select staff from customers for a customer in the cause demo to an adjective as the record that we just inserted and it did so we can very easily go out and do those two things inserting a customer noticed that when the deal with customer and their orders without really have to worry about the fact that potentially it does not happen the customer table but potentially the primary key here would be ought to generated as well and often in France s bit difficult to insert into orders because you don t yet know why either customers going to guess in this frankly doesn t really matter because

Minute 4

Just adding to the collection on the customer the new order that we want to add and then got uncles make changes at that point will work out what the key values are supposed to be sold in London realised that the framework doesn t know that it is to insert the customer before it can serve the customer s orders and guess that ordering correct now and naturally if we go ahead and violate constraints in the database them will get problems of what I recall submit changes to everyone rather less having tried to exert the same key twice within a problem here you can see that we going a sequel exception this particular case to you agreed to go ahead and catch those exceptions deal them if you doing inserts or under constraints within the database that can stop you doing into analysis cutback in clear out this demo customer surreal depicting the same problem so get rid of anything is in demo company several your honour we got rid of that

Minute 5

Are suddenly outlined the really interesting thing about this is that we go ahead and Whitbread is customarily added into the customers entity set up a collection if you like anyone you think that at that point there before we have into the database if we were to go on query that collection let s call this new customers of the neither Scotland new customer let s go to context customers get the neither single customer where customer ID equals downward to because we just added into this collection here the customer demo twos you expect this fact would come back with the customer record that what is run exactly to currency were actually happens at a vital it is the

Minute 6

Code political profile address running in the background that is clear there out but while it is piece of code will look in into the context and as he had to look at its identity cash for an identity called them that it were any asked me if the something has come from the database of the primary key of demo to and what is essentially saying that point is that they haven t seen such as income from the dead so as we several line of global happen is to begin an exception in article server trace we just went to the database looking for a particular customer of course he s not there because we didn t insert him yet sold he can think I this is working what s happening is when we get to this particular Annacone here were reaching into the customer s collection of light looking for the demo to customer even though we ve gone and added him here he doesn t have to show opposite cash

Minute 7

Record from the database consequently filtered a first time get that record and it s not there is Peter Gold would naturally work after we get to submit changes with his resistant to hear and will stop at nothing to code again as rather than to their better finding every clear sequel server trace that is what they are now much that over a line of code and we will see that this time around we didn t go to the database because the framework lowers that record is now live in the database consequently we can get hold of that new customer that we inserted and look at it but I need to be aware of that as we create these things are happening to the County set they won t show up from things like this customer stable until we gone to submit changes even though you might logically think they should as it is right that the gun just a quick summary onto a quick video here missing that we can use the class table of teen artist

Minute 8

And assess itself as the customers he and he ordered that he will data context of the above table of tea and we got have a mayor in order to insert it into the database when you get to submit changes because thing that we can do it by having like a customer and having an order into that customers orders collection that will also cause that order to be inserted into the database and we thought that if we got some kind of conflict than we did in silicon exception and we saw that if we did insert something like a customer then we couldn t expect to go back to the context and query a customer by ID cards that ID has not been brought into the identity cash at that point so the frame and actually doesn t expect looking for it where it doesn t yet exist that can be done after we got submit changes that can t be done before because that changes them over the is very clear from watching the video now you got look more like there are

Minute 9

There are on yen URL of Durham Green it will provide you information on the link project including an equal a great love out a link in action net and encourage it go have a look about you like reading out and block and find they can t live on feedback on the Wittgenstein passion if we can mail me on my e mail address is just down the screens we can mail me plan and on fashion other than that and Ltd they could have lived on and hope to catch again

Click here to go to original video page

No comments: