Presenter:Mike Taulty
The title says it all - how do we do updates 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
Hello my name is Mike told you were from Microsoft in the UK and this is one of a number of short videos about a new technology that is coming with visual studio coping walkers that will be the next version of visual studio to follow on from 2005 new technology is called into sequel language integrated query to sequel and what it does is put the new language integrated query And that we seen a number of places in visual studio in walkers on to a new data access API and that it access API provides an object relation or mapping such that we can take relational records on a database table and immediately turn them into objects inside of our programme and right are against those objects however say this is one of a number of videos so if you haven t seen perhaps the introductory video and one of civilians about the data context and mapping capabilities it might be worth taking a look at some of us before proceeding with this video what would I software here is how we can may
Minute 1
Modifications to data was pulled back from the database I m here in visual studio codename August 18 last technology preview which at the time of recording the video was the latest version available publishing on the Internet now I have done in very many of the other sessions in this series like a simple console application into that console application icon landed in into single diagram Ahmad iritis added the customer table from Northland and it s worth remembering that this session that I have hacked my customer table in Northland Avenue conflict this is my own innovation and it s the record version column and all that is timestamp and will be using that when legal updates in order that the evening to sequel framework and the forces optimistic in currency model checking us on the to change the record prior to meeting you am not related to enter that in this particular session because we have a separate session to
Minute 2
To the topic of concurrency in the optimistic concurrency model that we have in linctus equal basis to average lights into the close by in this session to analytical directors with customer on it and we also have a simple piece of code and at the moment likely to code is doing is creating a data context language done in very many of the other sessions in the series now everyone to get on data from our database at school and pull out a customer from the database will have the health key customer because he s been one I really know it on the northern database so what are the customers table on a single record worthy ID is healthy that guess is that record from the database and if you want to change something about Alf Key must for instance change Alf Key has been in a different time region as for mIU region and as it discussed in previous sessions nothing happens really up until the point where we do context
Minute 3
And exchanges at that point the framework is going to gather up the changes we ve made to the data that it knows about the data has been associated with the context and it will generate a sequel in order to perform those updates for us and everyone billed as piece of code and run it I have a sequel server trace window kicking around in the background and will see that we did indeed has gone update customers table we change the region for alt key cisapride raspberry and we is the primary key to find out key and as it happens we ll is used the timestamp in order to enforce optimistic in currency and a descent took that either separate sessions on Oregon here now it is very interesting for people to think about how did the framework no doubt we had to change the region on the alt key instance of regard here had a development done that
Minute 4
Got look at the generated code at a look in here and will find region if I could have done ago to definition in visual studio which would work better if you look at what happens with this region I am in the generated code your sparks that it is firing property changing and property changed events this is the way that the code generation tools in eight the entity classes that were playing with her in our case region is a property or a field on customer and your notice that customer implement I notify property changing and I notify property changed this means that the framework the link to seek a framework and sync up to these events on these instances and consequently it can know full well might see something like the region when it is something like the region property changing so it s not magic that no territories to things changed
Minute 5
It s worth saying press this point that you don t necessarily have to implement I notify property changing and I notify property changed in order to your classes to take part in link to sequel you can not be allowed if you want to earn code generation tools are doing this for you so you may as well get the benefit of it but if you hand cranking your own classes you don t have to implement there is an bitter situation like that the framework will said she have to compare the object before and the object after honour of the field by field basis in order to determine what has actually been shown now if we return back to programme one of the things that is very interesting about the framework ears that were saying that nothing happens that we submit changes which really opens at the question of what happens if we go and read query the framework for this record again will we get the updated record will be getting a bit
Minute 6
Record from the database in the first instance that Sun has changed this to some other value but scoreless my other new regions will have that piece of code executing their allies were asked the framework again for the asking records will have them asked he again and walk of the context in Ascot for customers at single scene customer Ivy equals and would have set a breakpoint here so that we can see what the value of our fatigue in reflects in terms of its region because it has really changed and it is a query the context again to reject the new regional doing at the old region as combined as the singles at the trace of a given ability to select that face our pop back to this piece of code here I m just here as well and I specify I ve
Minute 7
So at this point we should have been to the database in order to it is the wrong window should have into the database in order to fetch asking and you can see we have gone and done that my primary key letter that out and got back to code and at least it will only make the change to Alf Key and we query again at the interesting thing about that querying again is if we go back to sequel server was innovative and Ashley the sequel server to the framework was smart enough to detect that it uses primary key value already consequently if we look at this record will see that we do indeed get the latest greatest version of the region property which we change just as a previous life are so so far we are consistent within a framework within programme in that wants with change something it stays changed and we see the change value however you might think that s a bit of a trek because the framework new
Minute 8
That it already happy out of the record and consequently it didn t do anything at and is put out of its identity cash to let and be a little bit more subtle sneaky around this and ponder framework into going back to the database so much changes be separate here unless the ahead and say for each customer see in the context of customers not to list so that forced the framework to go to the database and select all the customers and holders will have a look at the customers are coming back and will say if it south key there must have the right wife so once again let s rerun this in the presence of a trace without Langtry s winner at the moment was run down to do that to our making a modification because in the last these code within that specified in commitments changed so that is a change that is that at this point we ve been to the database wants in order to get
Minute 9
He just one record there is clear that race when they re a little longer and that breakpoint we should find that we have now been to the database again to select all the customers so the first query was just a select house key the second group was to select the entire customers table so you might be thinking well okay at this point then the framework will be giving us the old alt key record the one that hasn t yet been changed because it s just a table scan essentially together is the whole customers table however need famously smarter than and we have a look at the customer record here you ll see that the region has indeed been updated to my that new region saw a really saying here is that if you make modifications to data that you later retrieve through the same data context and that s very important to understand that we are using the same data context here for both of these queries and modifications and
Minute 10
Will always see the latest most up to date later as you have changed inside your programme not as it was in the database originally took programme always maintains consistency with itself from that point of view and they said it is very important to realise that this is a feature of the data contracts as a consequently if you had to three data context they would only be consistent within themselves that you would not get consistency across context that is very important to realise that what goes on you can see how that s working here and that no matter how we try and trick the framework it still gives us back the right answer with respect to the changes we ve made to the data now in terms of what would happen if we had more things in our diagram to play with at a look at the Stranglers table back on to the Suzuka in the past three the orders they were back on our diagram as you entered in things like how you would clear out relationship so residents if we wanted to change Alfie is such that he did not have
Minute 11
Any orders then looking at Leeds session and recorded on division will help financially those kinds of questions essentially we can walk up to Alfie s orders collection we can clear it then essentially that will result in a number of orders at records in the database being orphaned away from the ASCII parent record as we have them right now so if you look at our video with your division that profits are up what we do around modifications where we want to change relationships between prisons customer and their orders now and so far we ve made changes to records and have already queried from the database so ago I had a record year in a change of circumstances naturally where you want to make changes directors of existing the deficit you haven t recently queried or maybe ever queried on think of being little clear scenario where you return to their surprise later on that s posted back to a different server s updates to the database and that includes those records in the first place
Minute 12
Out the framework supports this sort will do a little time not got that scenario just in our simple consul application is alright inevitable for many changes of practical change change customer something like that and will go in this method was used as two days ago that we has copied will create a new customers seen arrant will then go ahead of villains and details on that for a passenger this record will be the customer ivy grown to change a lot of under Pasadena the record version archives and because that s handling optimistic in currency for my customers to the north and files of acids in this new region I think so what will do little construct a customer with the question rightly in my flat in second an angle and set the record version as well the fact of the constructive with the record version 2
Minute 13
And then finally wobbled it was that the regional agony through the region that we got past it is now the lullaby not have a constructor like this on customers will not have to provide one no customer in the generated friends is a partial class so I can add some bits to that class if I put mybecause the generated code is in myand wobbled it is also public partial class partial class customer and animal instructors listening and will have streamed a surprisingly vital road record version and I will go and serviced customer ID equal society list or record versions record version and I regret you constructor the law hours to hopefully can cripple as obvious like that as the Treasury can compile so already a change customer function receiver customer added a record version
Minute 14
So that we can get past the problems we encounter with optimistic and currency would imply that field with a new region with greater contact to greater customer we don t query the context for the customer we simply create it retained its region article submit changes and you we would expect this to a long work that is as because the context hasn t been told anything about customer record in the way that introduced us to the context it is a context that customers are attached and within that customer was just created to what attaches saying hits the context those this is a record that you should expect already finding that a face you have a query for you haven t inserted it is already there consequently any changes that make you I want you to make his updates within thesewers were and to simulate how this might work with another data context of the assumption s approach to recover one of those three of the data context
Minute 15
Forget the customer Alfie from that Althea to think that our thief imagine logically that we now send this to the client over one services of something like the later on he centres and information back so we want to call change customer and we will pass it in our case Featherstone IDE the record version and other lying new super regional something like that legion something like that are built that was clearer signals a trace spirit of their rest has run at and it looks like broke something that is very fair I think we felt it was in stance is something in my construction would have been associated in the other constructor citrate and new entities are
Minute 16
It was just a mistake in my building and construct a little and ratified again thus I found that initially was too long for the young for the customer I do so is to change it too sorry to the region to let other regional envelopes that if the region is finally billed as run again and we hopefully after a couple of false starts to get data sent across to the database I will see British royalty and is a trait which has been running all their long that we are in fact abetting customers are changing the region were doing the optimistic and crazy check of sewage primary key about it as you are managing to be there it is a query and then much later on on a different data contacts with in an update and the way in which we do an update is simply by creating the customer record making sure that we get the optimistic and currencies of right and only attach that newly created
Minute 17
The records of the context we can make changes to it and we submit the framework will spot those changes will add to the database and even though we never queried this record from this context we can still let updates based on the family got here of attaching the customer to the context as opposed to trying to insert it or query for in the first instance just a quick summary or looking at here we seen that we perform updates the data by getting out of the database making changes to the properties with Dr Atlas changes I d attract certainly generated code that the user will contract by property notification events and interfaces with authors and even have to do that and in that case what Frank do is a comparison on a few biphenyl basis against to object to work out what he secretly to centre the database was other consistent is maintained so only make changes to one of our record and only query from the same data can t
Minute 18
And that is very important to realise it s the same day to contact them only get that the context make sure that it gives back the same object that had previously modified so we don t get out of step with work that was done elsewhere within our programme and also sold us down the middle tier scenarios which update records and had a special necessarily querying them first session attach method that we have a table week ago and use that in order to make sure we can update even if we haven t queried in the first place now and you go more red wine there are enter a barn and the yen you are at there on screen they will provide you information on the link project including an equal and weblog ad link inaction net and encourage it go have a look about United in Arafat and blocks and finally if you aren t providing feedback on the specific enjoying the fashion e mail me on my e mail addresses if there on the screen to mail me out I had
Minute 19
In action other than that and renovating the battle of an annual to catch in the future
Click here to go to original video page
Monday, July 21, 2008
Video: Updates with LINQ to SQL
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment