State-of-the-art Dating which have Entity Build Key (continued)

State-of-the-art Dating which have Entity Build Key (continued)

Today why don’t we include a new model to our Internet API – the fresh Firearm . The theory behind it is that each and every Character will provides that Firearm and you will vice versa.

I am aware, this isn’t the normal part-to tackle games. Usually, might have multiple weapons in your index and you can equip you to definitely otherwise a couple of, but for this case, your RPG character merely will get one to weapon of course, if need to alter it, you have to lose one other.

Next, we have to create other property towards Reputation class, specifically the latest Weapon . Do you really already see how to perform a single-to-that relation?

While the i extra a unique design i also want to see it as a dining table regarding databases. So, we must are the Firearm classification given that DbSet into DataContext .

Alright, it seems like i currently had everything you ready towards the migration. So, regarding the critical we sort of dotnet ef migrations include Firearm and you will struck get back.

The kid/depending front couldn’t be determined to your that-to-that relationship ranging from ‘Character.Weapon’ and you will ‘Weapon.Character’. To recognize the child/depending section of the matchmaking, configure the brand new foreign secret assets. In the event that these navigations should not be a portion of the same dating configure him or her instead of specifying this new inverse.

Okay. Therefore, thus we must identify an explicit overseas secret assets. By using the latest foreign trick, the newest databases knows where to look towards relevant entity.

Within analogy, we require new Firearm to get the newest based side of the relationships. There should be a nature for this Firearm . It cannot exists without it. The character simultaneously can can be found versus a weapon .

We all know you to definitely by adding other property with the Gun classification, and therefore are the assets CharacterId as the an enthusiastic int . With the aid of one to discussion – using the C# group identity while the text “Id” – Organization Framework understands that this is the corresponding overseas trick for the character assets. That’s all currently.

On the Up() style of new migration file, we come across producing brand new Guns table additionally the definition of this new overseas key restriction.

Regarding the migration structure document, we and comprehend the definition of new relatives between Profile and Gun . Towards procedures HasOne() and you can WithOne() Entity Structure defines usually the one-to-you to relatives. On top of that, you will see the word the latest international trick and then have what the results are when the a nature will get removed. With OnDelete(DeleteBehavior.Cascade) the brand new Gun would be got rid of because of the Profile .

Put Weapons so you’re able to RPG Emails

We start with undertaking a special folder for the WeaponService and up coming create the user interface IWeaponService . So it software merely becomes one strategy entitled AddWeapon() , returning an excellent GetCharacterDto and you can delivering an AddWeaponDto .

For this, we would a separate Gun folder on the Dtos folder and you will create the C# category AddWeaponDto on services Term , Wreck and you will CharacterId .

2nd, we produce the WeaponService implementation category and implement the relevant software and range from the async search term into AddWeapon() strategy.

I also need an excellent constructor inserting the brand new DataContext , brand new IHttpContextAccessor to find the currently registered user and you will an enthusiastic IMapper . We require an individual to confirm the fresh new given CharacterId later.

Complex Dating that have Organization Framework Core (continued)

Concerning your utilization of the brand new AddWeapon() means i first initialize the fresh new ServiceResponse , come back it effect and you can currently manage a-try/hook cut-off where we place best opinions in the event from a different.

Now let’s talk about the new are stop, i earliest have the correct Profile in the databases. I availableness the fresh Characters about _framework , select the first organization into the provided CharacterId in addition to correct User with the intention that we understand it reputation really belongs to the fresh new already subscribed associate.

Simply to recap, we obtain the fresh new Id of your own latest affiliate because of the being able to access the new NameIdentifier claims worth about JSON web token.

However, whenever we got the best character , we can do a new Gun including, into the considering Term and you can Destroy well worth and now have lay the new Profile assets associated with the brand new Weapon like into character object i had on the databases.

By-the-way, we could enjoys extra a separate mapping on the AddWeaponDto so you can this new Gun type of, or we simply set these properties yourself here.

They comes regarding ControllerBase and we need certainly to are the functions [Route(“[controller]”)] , [ApiController] and you will [Authorize] with all the corresponding using directives.

Then we need an effective constructor which is injecting brand new IWeaponService . As always we are able to initialize so it community about parameter.

After that, i only need that Post approach, that is AddWeapon() with an AddWeaponDto once the a parameter and you may coming back the result of the fresh new AddWeapon() variety of the latest _weaponService .

Following we strive to help make a new weapon on the Website link , the fresh HTTP system is Article and remember new Agreement key throughout the heading into best holder token.

About your human body, definitely have fun with an awesome term to suit your weapon and a correct characterId for the logged-during the associate.

Running the call contributes to a profitable impulse. But it’s not too satisfying. We do not comprehend the weapon of the profile. Well, definitely, we don’t notice it, given that i have not additional the new gun for the GetCharacterDto .

We become a difference! Much less of use. However when i’ve a close look at Critical for the Artwork Facility Code, we are able to comprehend the following error content.

Therefore even confides in us the brand new Id of one’s profile. And so the one-to-you to definitely loved ones functions perfectly. To incorporate various other https://cdn.vox-cdn.com/thumbor/EWvpzUaiEQ8W709WvLu3TK8zCjU=/0x0:3000×1571/fit-in/1200×630/cdn.vox-cdn.com/uploads/chorus_asset/file/22301839/merlin_95787282.jpg” alt=”sugar daddies New Jersey”> firearm, we very first have to get rid of the Grasp Sword from the database table.

We perform another type of GetWeaponDto group you to definitely merely includes the new Term in addition to Ruin of one’s Weapon . It’s not necessary to own an enthusiastic Id or the Character here.

That’s it. When we today remove brand new gun in our reputation on database basic after which manage they once again which have Postman, we get the whole profile straight back – towards the associated weapon.