Today let us create an alternative design to your Web API – the latest Gun . The concept trailing this can be that every Reputation only has you to Weapon and you may the other way around.
I know, that isn’t their typical character-to try out video game. Always, you would has actually several guns in your index and you may make it possible for one to otherwise two, but for this example, your RPG profile just gets you to weapon assuming you need to evolve they, you have to miss additional.
Up coming, we need to include another property towards Reputation category, specifically the brand new Firearm . Can you already find out how to perform a one-to-one to family members?
Because the we added another design we would also like observe it a dining table throughout the database. So, we need to add the Weapon category since DbSet towards the DataContext .
Ok, it seems like we currently got everything you ready to the migration. Thus, about terminal i type of dotnet ef migrations create Weapon and you will strike come back.
The kid/centered top could not be determined to the one to-to-you to dating ranging from ‘Character.Weapon’ and you will ‘Weapon.Character’. To spot the child/oriented area of the relationship, configure the fresh overseas secret property. If the this type of navigations really should not be an element of the exact same relationships arrange him or her without indicating the brand new inverse.
Ok. Thus, as a result we have to describe a direct foreign trick assets. With this new foreign secret, the fresh new database understands where to search on relevant entity.
Inside our analogy, we truly need the brand new Firearm getting the fresh created region of the matchmaking. There must be a nature because of it Gun . It cannot can be found without one. The type in addition can also be exist in the place of a weapon .
We know one to adding other assets with the Firearm group, and that are definitely the possessions CharacterId since the an int . With the aid of that seminar – by using the C# classification term plus the text message “Id” – Entity Framework knows that this is the corresponding overseas trick getting the smoothness property. That’s all currently.
About Right up() sorts of the brand new migration document, we see producing this new Guns dining table and definition of the international secret constraint.
From the migration build document, we plus comprehend the concept of the brand new relation between Reputation and Gun . On methods HasOne() and you will WithOne() Organization Framework represent one-to-you to definitely family. On top of that, you will see the expression new foreign key and just have what goes on in the event the a character gets removed. With OnDelete(DeleteBehavior.Cascade) the fresh new Weapon would be removed using the Reputation .
Include Firearms to help you RPG Emails
I start by performing a new folder for our WeaponService and you can then produce the program IWeaponService . So it program merely becomes one technique named AddWeapon() , coming back an excellent GetCharacterDto and you can bringing a keen AddWeaponDto .
Regarding, we would another Weapon folder on Dtos folder and create the C# category AddWeaponDto with the qualities Identity , Destroy and you may CharacterId .
2nd, i create the WeaponService implementation group and apply the corresponding user interface and you may add the async keyword on the AddWeapon() method.
I also need an effective constructor injecting new DataContext , the fresh IHttpContextAccessor to find the currently subscribed member and a keen IMapper . We want the user to verify the brand new offered CharacterId after.
Advanced Matchmaking that have Organization Framework Core (continued)
About your implementation of the newest AddWeapon() means i first initialize the brand new ServiceResponse , go back so it effect and you may currently carry out an attempt/hook cut-off in which we put best values in the event regarding an exemption.
Now for the newest was cut-off, we very first get the proper Profile regarding the database. We availability the brand new Letters regarding _perspective , discover basic organization with the considering CharacterId plus the correct User to ensure that we know it character extremely is part of the latest already subscribed representative.
Just to recap, we get the newest Id of your newest member of the being able to access the new NameIdentifier says well worth regarding the JSON online token.
However, if we got ideal reputation datingranking.net/sugar-daddies-usa/nv/ , we could manage yet another Weapon such as, into given Identity and you can Destroy value and have now put the fresh Character assets of new Weapon such as for instance on the character target we had on databases.
By-the-way, we are able to possess additional a unique mapping on AddWeaponDto so you can the Firearm form of, otherwise we just place these qualities by hand right here.
It derives regarding ControllerBase so we need add the services [Route(“[controller]”)] , [ApiController] and you will [Authorize] with all the associated using directives.
Up coming we are in need of a beneficial constructor that’s injecting the brand new IWeaponService . As usual we can initialize so it career on parameter.
Following, we only need one Blog post strategy, which is AddWeapon() having an enthusiastic AddWeaponDto because a factor and returning the consequence of the brand new AddWeapon() kind of the _weaponService .
Then we strive to produce a special weapon to your Website link , the newest HTTP experience Blog post and don’t forget the fresh new Agreement secret regarding the header for the proper holder token.
Concerning your human anatomy, make sure you explore an awesome name for your gun and a correct characterId to suit your signed-in the associate.
Powering the call leads to a profitable response. However it is much less satisfying. We do not comprehend the firearm of the character. Really, definitely, we do not find it, as the i haven’t added the weapon with the GetCharacterDto .
We get an exemption! Not too useful. However when i’ve a close look in the Terminal in the Graphic Business Password, we could comprehend the following mistake message.
Also it also confides in us the Id of the reputation. So that the you to-to-you to definitely loved ones work perfectly. To add other weapon, we first need get rid of the Master Sword in the database desk.
I create an alternate GetWeaponDto class you to definitely simply includes new Title additionally the Ruin of Weapon . Its not necessary to have an Id or perhaps the Reputation right here.
That’s it. As soon as we today remove the latest gun your profile on the databases very first then create they again with Postman, we get the whole profile back – with the associated weapon.