EF 7 - New JSON Columns

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign we're going to talk about the new Jason columns available in Entity framework 7. this is great when you need to save a specific type of data in an entity without creating another complete table or using a weird custom type now you can save a small stuff like details tags or whatever you want into acolume that will contain at this Json structure and it will be automatically serialized by Entity Framework the great thing is that it works with most of the of the providers of Entity framework wherever database you're using because if there's now an idiot get some time available it will use the max bar chart available to save these Json so let's see how we can implement this and how we say this to use it okay first things first we need the latest version available of Entity framework 7 and Dot net 7. for all these functionalities to be available for us so keep keep updated try to have the latest beta or the latest release candidate that it's available or the release if you're already if you're seeing this video by the time of release so okay let's define a use case here I have my car entities the same from last last videos probably a project maybe you can check it out but okay I have my car and I want to have the the details of the car if it has these are used cars and I wanted to I want to know if they have a scratch or a defect on the paint whatever any type of detail so normally what what we will do is we need to create an entity for it detail CS normally we could use also maybe a list of string of strings or something like if just to have the descriptions of every single detail that it has but right now into the framework 7 doesn't support native type collections like we need to create an entity with properties inside so and and that can be serialized but it will not serialize if you use a list of string or integer or whatever so that type of stuff which are it's not available yet the same way that we don't have the data annotations for these implementations but okay let's keep going by creating our entity that will represent the details of the car namespace otherwise issues models so this makes sense only the ID because I will not use relationships for this it will not it will be something with our table it will be a column will that based on the provider that we're using for a database our database more engine it will variate to use an inverter Max barter Max or bar chart to Max or whatever maybe even the Json type if it's available on on your database but okay now we have our detail entity and we can say it on our car so we said public public list of detail which we will be details I'll need to add this Constructor here good practices here and okay now we have a list of details in the normal way that Entity framework will work if we don't have anything to set up this entity they will create a a table called details and there it will make their the whole relationship foreign keys and everything to make this work when you add when you add a car and it will work almost the same way but with relationships that will re that will require a Cascade deleting and a lot of stuff right so we don't need that let me change this to models uh it was wrong and yes we'll need that so let's make this property here from the car an array of Json that will contain the detail structure serialized in a column so okay this will be we if I don't do anything else it will be created as I told you as a new table with relationship to car as because we don't need that we can make uh on the on model creating or creating a an entity type configuration class and you can also Define this on an entity type configuration class but here for the example I will be doing it on the on model creating so let me show you how you can set up a child of your entity to be a Json cologne so model builder we find that the entity that will be the parent The Entity that owns the the type so in this case will be car so now that I have this I can set owns many because it has many details you can set it as owns one the the ounce is it's something really that's been available for a long time in Entity framework and you can use it to set up the structure of the navigation of the child properties on on any entity that you have so we have to Define their own many details X details and then we have the navigation definition this will let let us Define what's going to happen with the entity type that will be created for that child you can Define it at the in a in a different entity definition like outside or you can Define it right here because we don't have an entity for it we need to like quick maybe it works I haven't tried it like model builder entity details I think it will not work because this is not a real entity a real table for it for it it cannot be tracked in any way so you need to do it by using the navigation parameter on the owns many of the of the parents so navigation and here we can add some stuff we can define a table and the table name that will contain it but because we don't want a table we have a new functionality that's called to Json so navigation to Json so as you can see here now the entity car owns many details and it will be accessed via Json it will serialize anything that you have into the details to be used as a Json that's it it's as simple as that and in the future it's already on the GitHub actually we should be able to use this as a data annotation on top of of your list so if you have a list you can add the data on this annotation Json probably and it will do the same thing so you don't have to Define if it's a table just split there and it's really simple and it because it's from system data annotations it will not represent a dependency inconvenience if you have multiple layers because it's coming from system and it's available on the.net standard so it's great when we I'm hoping we we will have The annotation sooner so great now that we have our definition of how it's going to be accessed let's try this out I already have here an ad markup data a script it will add 100 entities and yeah like it should work the same way I'm just adding a lot of details to this and we will see on the other ways how this is serialized and then we will try to request some data so we can test this out so okay let me just clean up my my project just in case dot net run if it has an issue sorry I just developed this so great it seems like everything worked and I had on my on my control my Constructor the ensure delete and should ensure created and then add mock-up data so I will always have new mockup data whenever I instance by the other ways but here I have my database as you can see I already had some requests to it and yeah we have here our cars and as you can see the the details column it's a Json that contains the structure of my detail a model that I have on my models folder and everything seems to be working working fine now I have my cars I don't have any more tables or unnecessary tables with that I don't have any many more relationships to it and it's a really small set of data that actually doesn't have dependencies if I delete anything or even you can have different structures inside it's more Dynamic you don't have that tidiness that we have with the relationship with the relationships on another way so great and that's it you see how easy it is to convert a child entity into a Json that doesn't require a stable relationships constraints and all that stuff that we have in relationship relational databases now we have Dynamic data structures that we can preserve on acolume great for when when we don't want to have migrations or we just need to save a couple of values or very structural change in data that might change in the future and then you don't want to mess up with the structure of your database your relationships and everything so it's really cool and it has all the features that are available by using tables right now with the release candidate it has some issues and bugs that are going that are being solved but by the final release you should be be able to run all the linking statements are you that that you're used to use in with your Entity framework applications so that's our wrap up for this topic so don't forget to press the like button if you like this video I hope you enjoy it and you can try out these pictures and comment if you'd like to see more or you if you have an opinion about this don't forget to see our older videos and subscribe if you want to see all the new ones without anything else to say happy calling bye bye
Info
Channel: Hahn Software
Views: 3,538
Rating: undefined out of 5
Keywords: #C, TypeScript, Angular, .NET, dot net, JavaScript
Id: X1YdfeLWvDc
Channel Id: undefined
Length: 10min 57sec (657 seconds)
Published: Fri Oct 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.