Complex Dating with Entity Build Core (continued)
Today let us create a different sort of model to our Web API – this new Weapon . The idea behind this might be that each and every Profile only keeps that Firearm and you will vice versa.
I’m sure, this is simply not their regular part-to relax and play games. Usually, you would possess several weapons on the directory and you can equip you to definitely or two, but for this example, their RPG character simply gets one weapon assuming you would like to evolve it, you must lose one other.
Next, we need to include several other property with the Character group, specifically new Firearm . Could you already find out how to manage a-one-to-one to family members?
Because i extra yet another model i also want to see it as a table regarding the database. Therefore, we must are the Weapon class as the DbSet to your DataContext .
All right, it looks like we currently got that which you ready toward migration. Thus, regarding terminal i type dotnet ef migrations create Firearm and you may struck come back.
The child/based front cannot end up being determined on the you to definitely-to-you to relationship anywhere between ‘Character.Weapon’ and ‘Weapon.Character’. To understand the little one/depending region of the dating, arrange this new international trick property. When the these types of navigations shouldn’t be area of the exact same matchmaking configure him or her versus specifying this new inverse.
Okay. So, as a result we need to identify a specific foreign secret property. With the fresh new international trick, the database understands where to search to your related organization.
Within our example, we are in need of this new Weapon becoming the built area of the dating. There must be a character for it Gun . It cannot occur how to use angelreturn without it. The smoothness as well is also are present in the place of a tool .
We know that by adding other possessions into the Gun class, and therefore would be the assets CharacterId since the an int . With you to definitely seminar – by using the C# classification identity as well as the text message “Id” – Entity Build knows that here is the corresponding international key to possess the character assets. That’s it already.
Regarding Upwards() sorts of the fresh new migration document, we see the production of brand new Firearms table and also the definition of brand new foreign secret limitation.
Throughout the migration build file, we including understand the concept of the brand new family relations ranging from Profile and you may Gun . Towards the methods HasOne() and you will WithOne() Organization Build describes one-to-one relatives. On the other hand, you can view the definition of the newest foreign trick and then have what happens if a character gets removed. With OnDelete(DeleteBehavior.Cascade) the brand new Firearm could be got rid of using Profile .
Include Weapons so you’re able to RPG Letters
I start with starting a different sort of folder in regards to our WeaponService and you will next create the software IWeaponService . So it program just will get one method called AddWeapon() , going back a beneficial GetCharacterDto and you will providing a keen AddWeaponDto .
For the, i do another type of Weapon folder regarding Dtos folder and you can create the C# group AddWeaponDto on the functions Identity , Wreck and you can CharacterId .
Next, i create the WeaponService implementation group and implement the latest involved program and you may add the async keywords on AddWeapon() method.
I likewise require an effective constructor injecting the latest DataContext , this new IHttpContextAccessor to find the already licensed representative and an enthusiastic IMapper . We need the user to confirm the fresh provided CharacterId later.
State-of-the-art Relationships which have Entity Framework Center (continued)
Regarding your implementation of this new AddWeapon() means we very first initialize this new ServiceResponse , come back which impulse and already create a try/hook cut off where we put ideal philosophy in the event regarding an exemption.
Now for the fresh new is actually block, i first get the right Reputation from the databases. We availability the brand new Letters regarding _perspective , find the earliest entity for the given CharacterId together with correct User so we realize it profile extremely falls under the new already registered affiliate.
Simply to recap, we become the newest Id of the current affiliate of the accessing the fresh new NameIdentifier states worth on the JSON online token.
not, when we had ideal character , we are able to do a unique Gun such as for instance, to the offered Identity and you will Damage really worth as well as have lay the new Character assets associated with the Firearm such with the character object we got from the database.
Incidentally, we could keeps extra another type of mapping from the AddWeaponDto so you’re able to this new Firearm sorts of, otherwise we just lay these functions manually here.
They comes away from ControllerBase and we need are the services [Route(“[controller]”)] , [ApiController] and you can [Authorize] using relevant using directives.
Next we truly need an effective constructor that’s injecting this new IWeaponService . As ever we could initialize so it job on the parameter.
Following, i only need one to Article strategy, that is AddWeapon() which have an enthusiastic AddWeaponDto as the a factor and you will going back the consequence of the fresh AddWeapon() sorts of the newest _weaponService .
Up coming we try to manufacture a unique gun with the Url , brand new HTTP experience Blog post and don’t forget the latest Authorization trick regarding header into right holder token.
About your body, make sure you fool around with a very good term for the firearm and you can a proper characterId for your logged-inside representative.
Running the call causes a profitable effect. But it is not very rewarding. We do not comprehend the firearm of your profile. Well, naturally, we don’t notice it, while the we have not additional the newest firearm for the GetCharacterDto .
We get a different! Not as beneficial. But once we have a closer look from the Terminal in the Artwork Facility Password, we can understand the adopting the mistake message.
Also it also confides in us new Id of the character. Therefore, the you to-to-one to relatives performs perfectly. To provide other gun, i basic need to eliminate the Master Sword about databases desk.
We carry out another type of GetWeaponDto class that simply includes brand new Title plus the Damage of Weapon . No need getting a keen Id and/or Character here.
That’s it. As soon as we now delete the newest weapon of our reputation throughout the database earliest and then manage it again having Postman, we have the entire character straight back – on relevant firearm.
قوانین ارسال دیدگاه