What's New in EF Core 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
from the uk we have arthur vickers he's our engineering manager for dot-net data i'm jeremy likness i'm a program manager for dot net data and i can't tell you how excited i am to be on campus to tell you what's new with ef core six with some help from my friend across the the big pond there so for those of you not familiar with entity framework core i want to give a high level overview because perhaps you know someone who speaks in code that's this robot here and it seems like every developer at some point wants to write their own blogging engine right so this is some blog code some classes defined but we also have data in our applications and our data master is probably going to speak a different dialect maybe t sql and describe the database this way what entity framework core does is it comes into the middle of everything and it provides a feature called a db context and this is really the glue between your domain model and your database and it allows you to fluently configure how those mappings work so this enables you to take advantage of many benefits these are things like writing strongly typed queries using language integrated query or link it allows you to work with your natural domain objects instead of weird property bags or tables or things like that it allows you to use a single command so what you can do is load a bunch of different related objects manipulate them delete them modify them and with one command save them to the database it will translate that for you we can fluently configure those relationships we can handle optimistic concurrency out of the box so multiple concurrent users is taken care of and we provide that api consistently across whatever back-end whether it's postgresql sql server cosmos db all of these are handled and supported and of course we recognize that database schemas do change so we provide a feature called migrations that handles those schema changes now what i want to talk about now is what's new because we packed a lot of functionality the team was hard at work for ef core 6. these are just a few of the items i'll have a link at the end of this presentation that'll take you to more detail about what it is now in the short time we have we're only going to be able to demo a few of these features i highlighted them there but before we jump into that i do want to talk about one feature in particular and that's performance so i'm excited to share that full stack all up ef core performance from ef core 5 running on the.net 5 stack the ef core running on.net on.net 6 has improved 92 percent now that's with the runtime improvements and improvements tf core massive improvement and if anyone's using dapper we love dapper it's an open source product that serves a similar need but if you're just going to it for performance reasons you might want to revisit ef core because we're within two percent on certain benchmarks of that query capability now the other thing i wanted to point out was compiled models and this is something that comes up when you have a really large model a lot of objects and relationships and you're starting up ef core goes out and looks at that model and figures out how everything is glued together we've provided the ability to do this statically at development time so with a very simple command you can introspect your model and you can generate code that will basically speed up your startup time so if you have a load balanced application if you're running multiple instances or if you're for example using azure functions and need fast startup this addresses that need but that's enough slides enough talk we've got so many features to show you i'm going to pass it over to arthur to show you some of these new features arthur take it over thanks jeremy so building on what jeremy said in his introduction i have a really simple model here i've got an order class which is related to a product and a customer we've got privatized constructors here private setters init only properties ef core is good at mapping to all different kind of structures you might have in your entities we have a customer again using a parameterized constructor in this case we're using a non-nullable string property and that'll automatically create a required property in the database uh ef core 6 is fully annotated for nullable reference types and we use them internally as well and then finally we have a product here and another thing that we've added in ef core 6 are more mapping attributes so for example we have a precision attribute where i can say okay when you map this to the data mate database make the precision of this decimal property 18 too so rather than having to do that in the fluent api you can just stick a precision attribute on it there and then tying it all together as jeremy said we have a db context and this is all the configuration that you really need for this uh just three db sets one for each of our tables customer product and order and then i'm using uh sql server local db uh and i'm also using implicit usings here and in my uh cs project i've added microsoft entity framework core here so when you're in the db context there's no additional usings necessary that keeps it really nice and clean okay so there's a couple of ways that you can use efco with databases you can reverse engineer from an existing database or you can use what we call afcor migrations to actually create and manage the database for you and i'm going to use migrations here to create this database now i have the dot net ef tool installed as a local tool in my project there it is and to create a migration i just need to use dot net ef migrations add and i just call it one for my first migration and that's going to create a migration builds things just to make sure everything is up to date once it's builded i will switch back to visual studio so we have a migrations folder and in here we have the migration that's going to create the tables now notice here that the string column is nullable false it's a required string column because that was a non-nullable reference type and the decimal is 18.2 as we configured it i'm also noticing that our strings are all by default configured for any length of string but sometimes you want to limit that so instead of using them varchar max let's say we want to use m varchar 200. so let me go back to my db context now the traditional way to do this let me just grab some code here the traditional way to do this is either to put attributes uh on each of the properties you have to do it for every property or alternatively to override on model creating like this so people have used af will be pretty uh familiar with this so this says for the name property make it 200 for the name property on product make it 200 and you do this for every property but we have a new feature in ef course 6 where there's another property a different pro different method rather that you can overload on your db context and it's called configure conventions and so for this what we're saying here is for all properties in the model of type string make the default max length here 200. so now basically we can get rid of all of this code and in one line we can say make the default 200 everywhere you can always override it with attributes or additional fluent ap specific properties but this lets you do common configuration including things like sending a value converter so i've made that change and i'm going to go back to uh my command here and i'm going to add a second migration so this is now going to change all of my string properties uh string columns to a max length of 200. now it's told me i've got some data loss that's because i'm narrowing the column here down from max length to 200 but since we don't actually have any data that's okay we don't need to worry about that so you can see here now i've got a second migration and we've changed the the columns to nvarcar 200. okay so traditionally with migrations you could now do a net ef database update and it will apply the migrations to the database new for ef core 6 is a thing called migration bundles so for that you do net ef migrations bundle and what this is going to do is create an executable file that's specific for the platform so i'm on windows so it's going to give me an xe if i were on mac or linux it would give me an appropriate executable for that platform you can see here we've got this executable this executable requires the dotnet runtime on the machine but not the sdk that's the default you can also build it as a self-contained executable which doesn't require anything to be installed on the machine that it runs on so this now has bundled up all of our migrations so i can use this in my cid cd pipelines in order to apply migrations to a database to start with i'm just going to apply i'm just going to run this to apply the migrations to my development database i have on machine and if i switch to sql server management studio here and refresh the databases you'll see that we have an orders database it's got the tables we expect with the columns that we would expect in each of the tables um okay so i'm going to come back to bundles in a second but i want to talk a little bit about um it sql server temporal tables so for support for sql server temporal tables is something that we added in af core six this is a feature of sql server that basically keeps your keeps track of every change you make to a database so when you update a row or you delete a row in a database in a database table sql server will take take a copy of the snapshot of that row before you make the change and put it into a side table called the history table and thereby keep track of everything that you've done to that table so basically you get an audit history of everything you've done so if we go to our context here we can tell ef core that we want all of these tables to be temporal tables by adding some configuration to on model trading so here i'm saying for customer make the table temporal likewise for product and order so i save that and then i'm going to go back to my my powershell window here and create a new migration for it just like we did before let's call this one three okay so now i have a third migration and i'm going to recreate the bundle to add that migration to it so i'm going to rerun the bundle command i'm going to give it a force just to tell it it's okay to overwrite the existing one and now i can run that bundle again or the new bundle actually so let's run that again and you'll see that it's going to apply migrations 3 to the database that we created and if we go back to sql server management studio and refresh the database and look at the tables we'll see that now these tables say system versions that means that they're temporal tables and if you look inside there's a history table with all of the columns duplicated without their constraints because this will have lots of additional rows for each primary key in it which we'll get back to later so now we have basically migrations has now created temporal tables for us in the uh in the database in fact if we go back to visual studio here and look at the migration you can see it did that by doing alter tables and creating the different parts that were necessary okay so i'm going to talk a little bit more about temporal tables in a minute um but first i want to um go back to bundles and finish what i was talking about there so we have a bundle that has three migrations in it now we put applied two to our development databases and then the third one but now if i've tested that and everything is great i can now take that bundle and without rebuilding it or doing any or making any changes to it i can just pass a different connection string to it so here i've got a connection string that's pointing to my production database and i apply that and even though i only applied the third one when i applied it to my development database it sees the production database needs all three and so it applies all three and this is the kind of power of migrations it keeps track of where your database is and updates it to the correct place and in fact if i just run this same command again it's going to say there's nothing to do it's already up to date so you can take this bundle put it into your pipelines for deployment have it update your production database and that's the that's the power of bundles okay so enough on bundles and let's go back and look a little bit more at temporal tables so the thing about temporal tables is that it works on history in other words you need to have some data in the database to make it uh useful and so in order for this to make this in the to show this in the demo i've just got this make history function and it puts a few products a delorean a flux capacitor and a hoverboard into the database then it does a thread sleep to wait a little bit changes the price same again change the price on the delorean creates an order and so on and so forth so with that we can now run a program here and i'm just going to comment out these other two lines here for now and we're going to make some history and then look up the current price of the delorean so let me uh run that and you should say okay we're making some history we did that and the delorean with this pk is currently a hundred and fifty thousand dollars a bargain um let's look at this look up current price so this is an important thing most of the time when you're using temporal tables you just use them as a normal table you don't really care what's happening what the history is and everything so you just in this case i'm just doing contact products single normal thing and just printing out things nothing fancy it's all the same however because we're saving all that data into that history table when we want to we can go back and look up the history of everything that changed and we have new link operators in ef course six one of them called temporal and that says bring back me all of the history so we're looking up for the history of the glory and we're going to look at all the changes so let me run that again we make some history again and we now get all of the prices of the delorean it went up in price to two and a half million and then back down and up again and you can see these all have the same pk because these aren't different rows or different entities this is the history of a single entity instance the history of your single row in that table there's other operators here so we have temporal from two which takes a date a range of date times uh and if i run that again it does basically the same thing as before except this time it's just going to limit the history it shows you to between those two times and there you go and then finally we have this operator temple as off and this one unlike the range falls takes a specific point in time and says give me what that table looked like at that specific point in time and because all of your constraints in your database are conserved at that point they're all valid you can also now bring in other parts of your model so you can say okay the order at that point in time and also what did the product look at that point in time and what did the customer look like in that point of time and so if we now let me make sure i uncommented this and then we'll run this again and we'll see that it looks up the order for when i ordered a delorean and it was two and a half million and my wife was not happy now this is just a very small part of a much bigger demo uh and chat that we had on the entity framework community stand-ups that we stream live every two weeks to twitch and youtube uh this is on youtube so if you want to see all the details you know what what happens with the from two the shadow properties all that good stuff is in the entity framework community stand up so i encourage you to check that out uh and now back to jeremy thank you so much for making history at dot net conf arthur love to make history absolutely so we made some history with azure cosmos db and added a bunch of new capabilities that i'd like to showcase so i'm going to pop over to my application i created my own blogging app and i have two blogs here one is this codes blog and as you can see i can come search for a thing and i'll get a thing and possibly another thing and then i told my friend brady that i'd show his face on the presentation so we got him into the talk here and he just has a love of dot net so i could click on these links and drill into it but the problem is this is all mocked code so let me take you behind the scenes and show you i basically made an interface for blog access now what i like about this interface is on the client and i'm using blazerwasm i can come into the client and implement that interface as a bunch of asynchronous web calls using http client so these are just calling out to a traditional controller if we go onto the server and look at the controllers let me just drag this out real quick the block controller is using entity framework or actually it's using my interface to get access and then the implementation of that access is just a fake c-sharp class i'm basically hard-coding the blogs so what i want to do is use a cosmos db database that i have real data and this is the data here we've got blogs with posts inside of the data and i want to wire that up so the first step i already took and that is adding a reference to entity framework core and you can see that reference is right here down in the under the packages reference so we've added that the second thing we need to do is make that glue between the domain model in the database so i'm going to add a new class and i'm going to call this creatively blog context and then to save some typing i've got a special snippet but you'll see that there's really not much to this when we do the blog context what we've got after i do my magical control period right to pull in the usings is we've got a constructor that takes some options for configuration and then we've got a db set for the blog this is all the configuration i need to wire this to the cosmos db database now the next thing i'm going to do is implement a quote-unquote real version of the blog access so i'm going to call this cosmos blog access and this one will be a little more involved but not too much so one of the things before i implement this if we look at the mock blog access you can see i'm using a projection here basically the blogs contain all of the posts and we don't want that to display we just want the blog header information so this projection just selects the pieces that we want now this is using fake blogs this is linked to objects but we can do the same thing with our actual interface to cosmos db and let me show that so for the blog access and again i'm going to do a few magical control periods here to bring in my usings i think i have one more at the top so what i'm using is a db context factory that's a configuration that i'll show you in a second that gives us an instance of that context i'm using the same link statement here to do the projection so that will get translated to cosmos db for me and only pull back the fields and properties that i want same thing here now for the filtered posts that was a much more complex query so i'm excited to share that no matter how complicated the query is if cosmos db can handle it we can translate it for entity framework core so here i'm taking raw sql and projecting it into that blog post so now i have the elements in place i just need to update one more piece here and we're going to add some configuration to actually wire up the blog context and my friend christos who's going to be on a little bit later is always harping on me about security that even in demos i should be aware of security so i wanted to make sure i got my keys from some completely secure location so we've taken care of that grabbed our keys and then i'm just going to use cosmos as the provider and the very last thing i'm going to do is instead of our mock blog access i'm going to switch this to the cosmos blog access we're going to hit f5 let that spin up and you know this trick right with the mouse clockwise always speeds up the build brings us to the screen and we can even help cosmos out a little bit and wind it up right here and now i've got these two blogs that are live from my cosmos database fact i remember there was a mentoring ring discussion that i can search for here's a engineer mentoring ring and on my personal blog i happen to be a huge fan of cosmos so if i type in the search term cosmos we can pull out all of the different blog posts that i've presented on cosmos so that is what i wanted to show you there now arthur mentioned that we have some community stand-ups that we host pretty much every other week and we have different guests on and we talk about all things data related you can access those with the link on your screen you can also see what's new with ef core and at that i'm going to answer some questions i believe and welcome my friends jamie and scott back hello hello [Music] thank you for putting in the work with us today my pleasure let's go to the big board here arthur's the one who made the history though so what's that i said arthur is the one who made the history okay thanks arthur appreciate that all right let's see here uh metadata has a question do we gain the same 92 performance with other providers like postgres provider so it was actually the postgres provider that was used we're using the industry standard fortunes middleware benchmark and that uses the postgres driver so that's exactly where that performance came from wow okay that's a way to make the point very clearly yes so cross-platform and running on postgres not benchmarked on sql server for that particular benchmark correct cool congratulations that's a that's a retort i didn't expect that's really great um what's this one here temporal temporal temporal table temporal temporal table sound awesome what is the cost memory display cpu there's always yeah i wish i had the memory and disk space off the top of my head but it is a feature of sql server so regardless of if you implement it in ef course taking the same hit and because it does have extra rights the rights do have overhead but sql server is really good right at managing this at scale so it in most cases should not have a major impact on what people are doing okay that's cool that's cool what else we got here jamie yeah we got a few more where's that compliment oh um yeah an appreciation in there there's an appreciation there oh here we go where we got we got a good question though uh is the sql project file still supported in vs22 without using af core migrations our complex migration scripts also supported in ef core migrations and greetings from germany that is a lot bundled in there do we still have arthur arthur do you want to take that i am here yes um yes so uh yes uh we support you know a database first workflow so you don't have to use migrations um there's actually some really good blog posts by one of our great community guys eric ej that shows how to use project files and dac files with sql server so you can still manage all of that stuff as far as the complexity of the migrations we support some pretty complex stuff but anything you don't need you can just put your own raw sql into your migrations and pretty much cover anything that you could possibly do in sql that way all right that covers it all multi uh multiple questions multiple answers this one yeah it looks like a compliment here automatically recording usable history people appreciate the temporal tables and new easy link methods nice patrick likes that and what is jacob asking here jacob asks uh exe uh building xe what i'm trying to these things fit into tweaks into tweets rather it's hard to tell yeah and all of those acronyms more of uh hmm is it better than a so again um backpacks are still supported uh they have a kind of different flow um but if you want to use a backpack that that works great i believe they're still sql server specific ef call migrations work sqlite mysql postgres all of the databases and so it has that basically whatever works best for your particular application and your particular development flow that you're using you know you can use anything okay so use what makes you happy is the answer what i'm sorry right there yes all right it looks like somebody took a screenshot they were so excited about the uh 92 improvement in requests per second just by upgrading and again we heard on postgres is what you did that on that's what we did that on but it includes not just ef core enhancements to the query pipeline which all of the providers take advantage of but it also includes the runtime improvements so just going from.net 5 to dotnet six we saw massive improvements in several areas and so that is something we take advantage of right with the new product that's actually a great point because you know stephen tobe had that really long blog post a couple of weeks ago on net six performance and that makes that kind of reminds us all that when performance improvements happen at that low level then everyone who sits on top of it gets to ride that wave whether it be spam of t or low level things in the garbage collector or the jitter everybody wins in this case we don't know what percentage of those you got for free but a lot of cool benefits that you could sit on top of and then squeeze even more performance out of that absolutely so we we actually have a community stand up from a few months ago where shai who's our main postgres developer and also on the ef core team does a lot of perf work and he went into all the detail of the different components of that you know which bits came from the frameworks getting faster which bits from the postgres low level driver getting faster which bits from ef core getting faster and which bits from just optimizing the way we we use the benchmarks for example so if you want to learn all that detail it's it's right there on youtube to go find so that was a uh community stand up that we did uh how long correct yes all right uh it was during there during the ef course six time frame so i'm thinking maybe two or three months ago okay yeah cool and that ef stand-ups link has the whole list and the playlist and there's also a blog post from one of the releases that goes into details too yeah that's a good reminder that folks can go to dotnet.netdot.net live right we've got the tv so you go to the dotnet website and you just click in the upper right hand corner there there's actually a little chiclet that says live tv and in fact when that is pulsing red there's something live happening and whether you want to talk to the ef team directly and hang out with them spend time with them or any of the other teams that you've been seeing today and throughout the conference pretty cool thank you so much for your time thank you appreciate it thank you
Info
Channel: dotNET
Views: 26,950
Rating: undefined out of 5
Keywords: .NET
Id: _1fJeW4F3ts
Channel Id: undefined
Length: 28min 32sec (1712 seconds)
Published: Wed Nov 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.