Posted on

Advanced Relationships with Entity Design Center (continued)

Advanced Relationships with Entity Design Center (continued)

Today let’s create another model to the Internet API – the brand new Gun . The idea about that is that each Reputation will only has one Weapon and you will vice versa.

I’m sure, it is not the typical part-playing video game. Always, you’d has several firearms on your inventory and help you to definitely otherwise several, but for this example, their RPG character just gets you to firearm while need to change they, you have got to drop another.

Up coming, we should instead create various other assets with the Profile category, particularly brand new Weapon . Do you already learn how to do a-one-to-one loved ones?

Due to the fact i extra a different model i also want observe it a dining table in the database. So, we must range from the Firearm class once the DbSet to the DataContext .

Okay, it appears as though i already got everything ready with the migration. So, on the terminal we form of dotnet ef migrations create Gun and you will hit come back.

The kid/built front side couldn’t become computed towards the that-to-one matchmaking anywhere between ‘Character.Weapon’ and you can ‘Weapon.Character’. To recognize the child/based region of the dating, configure the newest foreign key assets. If this type of navigations should not be an element of the same dating arrange them in the place of specifying new inverse.

Ok. Thus, consequently we need to establish an explicit international key possessions. With the help of the international key, new database understands where to search to your related entity.

In our analogy, we require brand new Gun to-be this new mainly based section of the matchmaking. There needs to be a characteristics because of it Gun . It can’t can be found without one. The smoothness on the other hand is also exists without a tool .

We understand one with the addition of some other possessions on Weapon class, and this are definitely the possessions CharacterId since an int . With one to conference – utilizing the C# class label plus the text “Id” – Entity Build understands that this is basically the corresponding overseas key getting the type assets. That’s it already.

On the Upwards() form of new migration document, we come across the manufacture of the latest Weapons table and the definition of new foreign trick limitation.

Regarding the migration structure document, i in addition to see the concept of new loved ones between Reputation and Weapon . Into the methods HasOne() and WithOne() Entity Structure talks of usually the one-to-you to definitely family members. Additionally, you will find the phrase the foreign trick and just have what are the results if the a characteristics will get got rid of. Which have OnDelete(DeleteBehavior.Cascade) this new Weapon will be removed using Profile .

Include Firearms to help you RPG Characters

I begin by doing a different sort of folder for our WeaponService and you may up coming create the user interface IWeaponService . That it interface merely will get one strategy entitled AddWeapon() , coming back good GetCharacterDto and getting a keen AddWeaponDto .

For the, we manage yet another Gun folder regarding the Dtos folder and you will create the C# class AddWeaponDto toward qualities Label , Ruin and you can CharacterId .

2nd, we create the WeaponService implementation group and implement the fresh new related program and you may range from the async key phrase with the AddWeapon() strategy.

I also need an excellent constructor inserting the latest DataContext , the brand new IHttpContextAccessor to find the already authorized member and you will an IMapper . We want an individual to confirm the fresh new given CharacterId afterwards.

Cutting-edge Relationships that have Organization Construction Core (continued)

About your utilization of the AddWeapon() approach i basic initialize this new ServiceResponse , come back this effect and you may already create a try/connect cut off where i lay best beliefs in the case of a difference.

Now for the fresh try take off, i basic have the proper Profile on the databases. We access the new Letters on _framework , discover the basic organization towards the considering CharacterId together with right User in order for we realize this profile really is part of the new already licensed user.

Merely to recap, we get the fresh Id of most recent user from the being able to access this new NameIdentifier claims well worth about JSON net token.

Yet not, whenever we got the right profile , we could perform another type of Weapon such as for instance, with the provided Identity and Ruin worth as well as have set the fresh new Reputation assets associated with the the fresh new Gun such to your profile object we had about database.

In addition, we are able to features added a different sort of mapping on the AddWeaponDto so you can brand new Weapon style of, otherwise we just set both of these attributes by hand here.

It derives out of ControllerBase so we need range from the functions [Route(“[controller]”)] , [ApiController] and [Authorize] aided by the corresponding having fun with directives.

After that we truly need an effective constructor that’s inserting the fresh new IWeaponService . Bear in mind we can initialize so it community regarding factor.

Upcoming, i only need one Article approach, that is AddWeapon() with an enthusiastic AddWeaponDto just like the a parameter and you will returning the consequence of new AddWeapon() variety of the latest _weaponService .

Next we strive to create another weapon to the Hyperlink , this new HTTP experience Article and remember this new Agreement secret on the heading with the correct holder token.

About your system, definitely use a very good term to suit your gun and you may a proper characterId to suit your signed-into the user.

Powering the phone call leads to a profitable response. But it is much less fulfilling. We don’t see the weapon of one’s reputation. Well, naturally, we don’t find it, due to the fact i have not extra the newest weapon to your GetCharacterDto .

We obtain a difference! Not as of good use. Nevertheless when i have a close look at Critical in Graphic Studio Code, we are able to understand the after the error message.

And it even tells us the latest Id of your profile. Therefore, the one to-to-you to family members functions alright https://datingranking.net/be2-review/. To include another firearm, we basic must take away the Grasp Sword on the databases table.

We create an alternate GetWeaponDto group you to definitely just contains the brand new Label while the Destroy of one’s Gun . It’s not necessary having an Id or the Character here.

That’s it. When we today delete brand new gun of our character on databases first and then do it again having Postman, we become the entire reputation straight back – on related gun.