Now let’s put an alternative model to the Web API – brand new Firearm . The concept about this really is that every Character will simply features you to Gun and you can vice versa.
I understand, this isn’t their normal character-to experience online game. Usually, you would have numerous firearms in your list and you may make it easy for one or a few, but for this example, their RPG profile simply gets you to definitely firearm assuming you need adjust it, you have to drop additional.
Next, we should instead add another property for the Character group, particularly the newest Firearm . Is it possible you currently find out how to manage a-one-to-one to family relations?
Just like the i added another type of design i also want to see it as a desk from the database. Therefore, we have to are the Firearm category given that DbSet with the DataContext .
All right, it appears as though i currently got what you in a position on migration. So, regarding the critical i sorts of dotnet ef migrations include Gun and you may strike come back.
The kid/established front side couldn’t end up being computed on one-to-one relationships between ‘Character.Weapon’ and you may ‘Weapon.Character’. To identify the little one/mainly based side of the dating, configure this new international key assets. In the event that these navigations really should not be a portion of the same relationship arrange them instead https://datingranking.net/sugar-daddies-usa/in/michigan-city/ indicating new inverse.
Ok. Very, thus we must determine an explicit international trick assets. With the international trick, the fresh database knows where to look on the associated organization.
Within our analogy, we want the fresh new Weapon becoming the fresh established area of the dating. There needs to be a character because of it Firearm . It can’t can be found without one. The smoothness at the same time is can be found in place of a gun .
We understand you to by the addition of another property into the Gun category, and this are definitely the possessions CharacterId since an int . With the help of that conference – utilising the C# group title and text message “Id” – Entity Framework understands that this is basically the related overseas secret getting the character property. That’s all currently.
Regarding Upwards() sort of this new migration file, we come across the manufacture of the new Firearms desk while the concept of the fresh foreign key constraint.
Regarding migration structure file, i as well as see the definition of the latest family members anywhere between Reputation and you may Weapon . On the actions HasOne() and you will WithOne() Entity Design describes usually the one-to-one to family members. Additionally, you can observe the expression the fresh international trick and just have what the results are when the a nature becomes got rid of. Which have OnDelete(DeleteBehavior.Cascade) the new Gun will be eliminated with the Profile .
Put Guns in order to RPG Letters
I start by undertaking an alternative folder for our WeaponService and you may up coming create the user interface IWeaponService . That it interface merely will get one strategy entitled AddWeapon() , returning good GetCharacterDto and you may providing an AddWeaponDto .
For that, i carry out a separate Gun folder from the Dtos folder and you can create the C# category AddWeaponDto toward qualities Term , Destroy and CharacterId .
Next, we produce the WeaponService execution group and implement brand new associated user interface and you may range from the async search term for the AddWeapon() means.
We in addition need an excellent constructor injecting the fresh DataContext , brand new IHttpContextAccessor to find the already subscribed user and you can an IMapper . We require an individual to ensure the latest provided CharacterId after.
State-of-the-art Matchmaking with Organization Construction Core (continued)
Concerning your utilization of new AddWeapon() approach we first initialize the fresh new ServiceResponse , go back that it response and you will already create a go/connect cut off in which we put best thinking in case of an exemption.
Now for new is actually take off, i first get the right Profile from the database. I availability brand new Letters on _framework , discover basic entity to the offered CharacterId and the best Member to ensure we know it character really belongs to the already registered user.
Just to review, we become the new Id of most recent associate from the accessing the newest NameIdentifier states worthy of about JSON net token.
Yet not, whenever we got the proper character , we are able to create a separate Weapon like, to the given Title and you may Ruin well worth as well as have lay the new Profile assets in the the fresh new Firearm such as for example on profile object we got regarding database.
By the way, we could has actually extra a separate mapping on AddWeaponDto so you can the latest Weapon type, or we simply set these characteristics yourself here.
It comes of ControllerBase and we also need certainly to add the attributes [Route(“[controller]”)] , [ApiController] and [Authorize] with the related having fun with directives.
After that we need an effective constructor that is inserting the latest IWeaponService . Of course we could initialize so it occupation about factor.
After that, i just need that Article means, which is AddWeapon() that have an enthusiastic AddWeaponDto once the a parameter and you may returning the result of this new AddWeapon() type the new _weaponService .
Following we strive which will make an alternative weapon toward Hyperlink , the fresh new HTTP experience Blog post and don’t forget the new Agreement key on header toward correct bearer token.
Regarding your human anatomy, be sure to play with an awesome term for your firearm and a correct characterId for the signed-when you look at the representative.
Running the call causes a profitable effect. But it’s not as fulfilling. We do not comprehend the gun of one’s reputation. Really, however, we do not see it, since the we haven’t extra the brand new gun to the GetCharacterDto .
We obtain an exception! Much less of use. However when i have a closer look at the Terminal during the Artwork Facility Password, we can understand the following the mistake message.
Also it even confides in us the new Id of one’s character. So that the one to-to-you to family members works alright. To provide various other gun, i basic must remove the Learn Blade from the databases table.
We manage a separate GetWeaponDto classification you to just includes the latest Label plus the Damage of Gun . Its not necessary having an Id or even the Profile here.
That’s all. As soon as we today remove the newest firearm of our reputation on database basic immediately after which do they once again that have Postman, we have the complete character right back – to the related weapon.