Advanced Dating that have Entity Design Key (continued)

Porseleinschilderes

Advanced Dating that have Entity Design Key (continued)

Advanced Dating that have Entity Design Key (continued)

Now why don’t we add an alternate design to your Websites API – the Firearm . The idea about this will be that each Profile will only have one to Firearm and you may vice versa.

I know, this is not their regular character-to relax and play online game. Usually, you might enjoys several guns on the index and you may allow one or two, but also for this case, your own RPG reputation just will get one weapon assuming you prefer to evolve they, you have to shed one other.

Upcoming, we need to incorporate other assets into Character classification, namely the newest Firearm . Might you currently see how to perform a-one-to-that relation?

Because we extra a special design we would also like observe it as a dining table regarding databases. Thus, we Albany NY sugar babies should instead add the Weapon category since DbSet for the DataContext .

Ok, it looks like we already had what you ready on the migration. Therefore, on critical i particular dotnet ef migrations incorporate Gun and you can strike come back.

The child/based front cannot end up being determined with the that-to-you to definitely relationship anywhere between ‘Character.Weapon’ and ‘Weapon.Character’. To spot the kid/built section of the relationships, arrange this new international secret property. If the these types of navigations shouldn’t be the main same relationship arrange him or her versus indicating the brand new inverse.

Okay. So, consequently we need to establish a specific foreign trick assets. With the aid of the latest foreign secret, the newest database understands where to look on the relevant entity.

In our analogy, we require the Weapon becoming the brand new created area of the relationship. There has to be a character for this Weapon . It can’t are present without it. The type concurrently is also can be found versus a tool .

We realize one with the addition of several other possessions on the Weapon classification, and therefore may be the assets CharacterId once the an int . By using one seminar – using the C# class term and text “Id” – Organization Build understands that this is basically the relevant overseas trick for the type property. That’s it already.

Regarding Right up() form of the new migration file, we see the creation of the Weapons desk additionally the definition of this new overseas trick restriction.

In the migration structure document, we along with see the concept of new family ranging from Reputation and you will Firearm . To your methods HasOne() and you will WithOne() Entity Framework describes the main one-to-you to definitely family. On top of that, you can see the phrase this new international key and also have what the results are in the event the a nature will get removed. That have OnDelete(DeleteBehavior.Cascade) new Firearm would be removed with all the Reputation .

Include Weapons to help you RPG Emails

I begin by performing a special folder for the WeaponService and you can next create the user interface IWeaponService . That it program merely will get one strategy entitled AddWeapon() , returning an effective GetCharacterDto and you may providing an enthusiastic AddWeaponDto .

For that, we manage another Gun folder regarding Dtos folder and create the C# category AddWeaponDto towards properties Name , Destroy and you can CharacterId .

2nd, we create the WeaponService implementation classification thereby applying the newest involved user interface and you can are the async key phrase towards the AddWeapon() means.

We also need a great constructor inserting the latest DataContext , the IHttpContextAccessor to discover the currently authorized member and you may an IMapper . We want the consumer to verify the provided CharacterId afterwards.

State-of-the-art Relationships having Organization Structure Center (continued)

Concerning your utilization of the fresh new AddWeapon() strategy i earliest initialize this new ServiceResponse , come back so it reaction and you may already create a try/catch stop where we lay the right opinions in case out of a different.

Now for the latest are cut-off, we very first obtain the proper Profile on databases. We accessibility the newest Letters about _framework , select the very first entity towards the provided CharacterId and correct Member so that we all know that it profile very belongs to the new currently licensed representative.

Only to review, we have new Id of one’s most recent member by the being able to access the new NameIdentifier states worthy of on the JSON web token.

But not, whenever we got just the right profile , we are able to carry out a different Weapon such as for example, towards the given Identity and you may Wreck worth and just have put the latest Profile property for the new Gun such as toward profile object we had regarding database.

By the way, we are able to keeps additional a special mapping regarding the AddWeaponDto to help you the fresh new Weapon variety of, or we simply set these two qualities manually here.

It derives from ControllerBase so we need to range from the properties [Route(“[controller]”)] , [ApiController] and [Authorize] making use of the involved playing with directives.

Upcoming we require a great constructor that’s injecting the IWeaponService . Of course we could initialize this career regarding the factor.

Upcoming, i just need one Blog post means, that’s AddWeapon() having an AddWeaponDto because a parameter and you will returning caused by new AddWeapon() particular this new _weaponService .

After that we try to manufacture a separate weapon toward Website link , the latest HTTP system is Article and remember the fresh Consent secret regarding header into correct bearer token.

About your system, be sure to use an awesome identity for your gun and you will the correct characterId for your signed-in the user.

Powering the call contributes to a profitable effect. But it’s much less fulfilling. We don’t see the weapon of one’s character. Well, without a doubt, we do not view it, because i haven’t extra the fresh firearm toward GetCharacterDto .

We have an exception! Not too beneficial. But once we have a close look at Critical inside Visual Business Code, we are able to comprehend the adopting the mistake content.

And it actually informs us the brand new Id of one’s reputation. So that the that-to-one to loved ones performs perfectly. To add some other gun, i basic need take away the Learn Blade regarding the databases dining table.

I do a unique GetWeaponDto class that simply contains the brand new Label additionally the Destroy of one’s Gun . No need having a keen Id or the Character here.

That’s it. As soon as we now delete the fresh new gun of our own character from the databases first then carry out they once more having Postman, we get the whole profile straight back – toward related weapon.