EF Core 6 new features and changes for .NET 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
dotnet six was launched last month and within that there were a number of updates to entity framework core in this video we'll have a look at ef core six new updates we'll take a look at migration bundles temporal table support and pre-convention model configuration now for more asp.net core tutorials visit roundthecode.com subscribe to our youtube channel at youtube.com forward slash round the code and follow us on linkedin at roundthecode.com forward slash linkedin so we're going to first have a look at migrations bundle so i've got an asp.net core application open in front of us here we've got a couple of entities in here we've got a player and a team so we've got some properties within that and we've also got a db context now what we're going to do first is we're going to go to tools and we're going to go to nuget package manager and package manager console and we're going to add a migration for this player and teams for our db context so we're going to set the add migration and call it initial so that's going to generate a migration script for us as we can see here we've got our teams there and our players and what we can do with entity framework core 6 is we can go ahead and actually create an executable and this is handy for things like devops we can actually run the executable to update our database rather than run a powershell script the way we can do that is within the package manager console again we can go to bundle we can call bundle migration now what we're going to see here though unfortunately is we're going to get an error so it's going to go through the migration bundles but unfortunately we are going to see an error once it's finished building you can see there we've got a build failed we can use this verbose parameter to actually see the errors so we're going to go ahead and do that and see what what's going on so we're going to run that with the verbose parameter so we can see it's trying to build and now we're getting a could not copy the dll file so it's trying it 10 times and as we can see it's not succeeding it's coming up with build failed we've tried to run that in package manager console when it's not working so let's go ahead and try it in powershell we're going to use the net command line and what we're going to do is we're going to run the following so we're going to run net ef migrations bundle and we're also going to pass in the verbose parameter we want to see what exactly happens whether we get the same issue or not you can see he's trying it a couple of times and it's coming up with the same issue so process cannot access the file which is the dll file and it's trying that a few times but it's not succeeding by the looks of it as you can see build failed once again we've used the for base parameter which we've already done one way i managed to get around fixing this is to actually add the configuration parameter now if i use debug or release i still seem to get the same issue but if i put anything else other than that and so i'm going to put abc it seems to go ahead and actually do it for us and i'll show you that in a bit going ahead just building it now it's built up the executable file and there you go it's actually managed to create our migration bundle you can see in here it's created the efbundle.exe what this is going to do now is it's going to go ahead and update the database so rather than running the update database powershell script we can go ahead and just run this executable we're going to go ahead and do that that's done that let's have a look at our database now and see if it's actually updated so we've got this around the code ef core got our tables in there it's got our players and our teams so that's migrations bundle the next thing we're going to have a look at is temporal tables now temple tables allows you to keep a full history of data logging data within a sql server table it was introduced in sql server 2016 and with ef courses six it's now included support for that the way we can do that is if we come out here and we go into our db context what we can do here is we can go ahead and override the on model creating method passing the model builder so in that we've got our player table we use the two table method and then we just set in there that it's a temporal table like that and do the same thing for team as well now that we do that we need to add a migration because we've now set the tables as temporal we need to tell we need to sync it up with our database so we're going to add a add migration and we're going to call it temporal this is going to generate our migration script which we're going to run up to sql server you can see here it's done it with the team set it as temporal and it's done with the players as well let's go ahead now and update the database we're not going to use migrations bundle for this we're just going to call the update database command there we go you see that's now updated our database and if we go back into our database in sql server management studio we can see now that these are now set as system version so they're now temporal tables now what we're going to do is we're going to run some data and entity framework and show you how you can actually use the temporal command when using an entity framework query what we're going to do is we're going to create a team controller and i've already written the code so i'm just going to copy that over and create a new folder called controllers and we're going to set the team controller as the name of the controller i'm going to go ahead and copy all that paste that all in so what we're doing here is with this create sample method create a new team and we've added some players to that particular team with the update what we're going to do is we're going to change the team name so the team name up here when we create it is brighton but when we go down here we're going to change the name to brighton and hove albion and then with this get hyphen sample method we're going to go ahead for this temporal method where the id equals one which will be the team of brighton i'm going to go ahead and do that so we're going to run the api for us we're now going to load it up so there we go we're going to go ahead create our team of brighton and the players within it so that's returned a 200 response we're now going to update the team name from brighton to brighton and hove albion using our update sample method let's go ahead and create the 200. now we're going to go ahead and use our entity framework query using the temporal method to see the difference between the two as you can see here we've got a history of all the changes we've got it was initially brighton and now it's brighton and the whole albion like that we're now going to have a look at pre-convention model configuration and familiar users to entity framework core and using code first would know that if you set a string quite often it will come up with an mfr car max when we sync it up to the database schema with ef core 6 though we can go ahead and we can use pre-convention model configurations and this is where we can set a configuration which is the default we can overwrite it for specific properties but we can also set up a convention as well the way we go about and do that is we go ahead and we override the method which is configure conventions we need to pass in the model configuration builder of configuration builder and what we're going to do with this is we're going to go ahead and say with all the string properties we're going to set it as unicode and we're going to dictate that the it has a max length of 300 characters we go ahead and do that we now need to add a migration for that because we're just about to set the max length properties we're going to change them for each of the strings we go ahead we run the add migration command and what we're going to do is we're going to call it conventions as the migration script name that's gone ahead as you can see in our migration skits script we've got the team name it's been changed to 300 and also the surname and the first name of the players let's go ahead now and update the database for that that's gone ahead and done that so if we refresh our database if we go into one of the tables like the teams we can see now that the first name and surname now has a maximum length of 300 characters and the same with teams as well if we have a look at the name that's now 300 characters so there's some of the ef core 6 updates migration bundles looks promising but it's still work in progress and if you know how to fix the migration bundle command please get in touch as it's an issue i've spent hours trying to resolve now thanks very much for watching and hit a like on the video
Info
Channel: Round The Code
Views: 253
Rating: undefined out of 5
Keywords: EF Core, EF Core 6, Entity Framework, Entity Framework Core, Entity Framework Core 6, EF Temporal, EF Temporal Tables, EF Core Temporal, EF Core Temporal Tables, EF Core Migration Bundles, Migration Bundles, Entity Framework Migration, EF Migration, EF Core Migration, .NET 6, .NET 6 EF Core, .NET 6 EF Core updates, .NET 6 Entity Framework Core updates, .NET 6 EF updates, .NET 6 EF new features, EF Core new features, EF Core 6 new features
Id: pKp_4Qcm3z8
Channel Id: undefined
Length: 9min 43sec (583 seconds)
Published: Sun Dec 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.