State-of-the-art Relationships which have Organization Design Key (continued)
Now let us put an alternate model to your Net API – the brand new Firearm . The concept at the rear of it is that each and every Character is only going to features one to Gun and you can vice versa.
I am aware, this is not their regular part-to tackle game. Always, you’d has multiple guns in your directory and you will help you to or a few, however for this case, the RPG character simply will get one to weapon of course, if you prefer to change they, you have got to shed one other.
Next, we need to include another assets towards Character category, particularly brand new Weapon . Do you really already learn how to perform a-one-to-you to definitely relation?
Just like the we extra a different design i would also like to see it as a table regarding the database. So, we need to are the Firearm class because the DbSet towards the DataContext .
Okay, it appears as though we already had everything you in a position to your migration. So, regarding the terminal we particular dotnet ef migrations put Firearm and you will struck get back.
The child/depending front could not getting calculated to your one-to-one matchmaking between ‘Character.Weapon’ and you can ‘Weapon.Character’. To identify the child/founded section of the relationships, arrange brand new international secret property. If the this type of navigations really should not be a portion of the same relationships arrange her or him as opposed to indicating the inverse.
Ok. So, because of this we must define a direct international trick property. With the help of this new overseas key, the fresh databases knows where to search on the relevant entity.
In our analogy, we are in need of the fresh Gun to get this new dependent section of the dating. There must be a character for this Gun . It can’t exists without it. The character while doing so is also can be found in the place of a weapon .
We all know one to by adding various other property for the Gun classification, which would be the property CharacterId due to the fact an int . With you to definitely convention – with the C# category label together with text message “Id” – Organization Construction knows that this is basically the involved overseas trick having the type possessions. That’s it already.
On the Upwards() particular the newest migration file, we see the production of this new Weapons dining table and the definition of the latest overseas secret limitation.
Throughout the migration structure document, we also comprehend the definition of the newest loved ones ranging from Reputation and you can Gun . To the tips HasOne() and you may WithOne() Entity Construction represent the one-to-one to relation. Simultaneously, you can see the word the newest overseas key and now have what takes place in the event the a character becomes got rid of. Having OnDelete(DeleteBehavior.Cascade) new Firearm will be eliminated aided by the Reputation .
Include Weapons to RPG Emails
We start by carrying out a new folder for the WeaponService and you can then create the user interface IWeaponService . Which software only will get one strategy entitled AddWeapon() , returning a good GetCharacterDto and you can providing an enthusiastic AddWeaponDto .
For this, we create another Firearm folder on Dtos folder and you can produce the C# classification AddWeaponDto for the properties Identity , Wreck and you can CharacterId .
2nd, we create the WeaponService implementation classification and implement brand new involved interface and you can range from the async keywords towards the AddWeapon() strategy.
We also need a beneficial constructor injecting this new DataContext , this new IHttpContextAccessor to find the already registered associate and you will an IMapper . We truly need an individual to verify the newest offered CharacterId after.
Advanced Matchmaking having Organization Build Key (continued)
Regarding the utilization of this new AddWeapon() approach we first initialize the ServiceResponse , return which effect and you can currently perform an attempt/hook cut off where i place best opinions in case out of a different.
Now for the try take off, we first have the best Reputation about database. We availability brand new Characters about _framework , get the first entity into the considering CharacterId and the correct Affiliate so as that we understand so it reputation most is part of the latest currently signed up member.
Merely to review, we obtain the fresh Id of the current associate from the accessing the fresh new NameIdentifier states worth on the JSON internet token.
However, whenever we had just the right profile , we can perform a separate Gun instance, toward provided Name and you may Destroy https://datingranking.net/sugar-daddies-usa/mi/detroit/ well worth as well as have set new Profile property for the brand new Gun particularly on the reputation object we got from the databases.
In addition, we are able to have additional yet another mapping throughout the AddWeaponDto so you’re able to the brand new Firearm type of, otherwise we simply place these two properties manually here.
They comes of ControllerBase and then we need to are the attributes [Route(“[controller]”)] , [ApiController] and you may [Authorize] because of the associated having fun with directives.
After that we need a beneficial constructor that’s injecting brand new IWeaponService . Bear in mind we could initialize it field on factor.
After that, we just need you to definitely Article approach, that is AddWeapon() which have an enthusiastic AddWeaponDto due to the fact a factor and you can going back the consequence of the fresh AddWeapon() sort of the _weaponService .
After that we try in order to make a separate weapon on the Url , the brand new HTTP method is Article and don’t forget the fresh new Authorization key regarding header on the proper holder token.
About your system, be sure to have fun with a very good identity for your gun and you can the correct characterId for the signed-for the associate.
Running the call contributes to a successful impulse. But it’s much less fulfilling. We don’t understand the firearm of your own profile. Well, needless to say, we do not see it, given that i have not added the latest firearm towards GetCharacterDto .
We become an exception! Not too of good use. But once i’ve a close look on Terminal within the Visual Facility Code, we can understand the adopting the error content.
Therefore also informs us the new Id of your own reputation. Therefore, the that-to-one family works just fine. To add another weapon, i first need certainly to eliminate the Grasp Blade about database dining table.
We carry out a separate GetWeaponDto class one just consists of the fresh new Name as well as the Damage of Firearm . It’s not necessary for a keen Id or perhaps the Profile right here.
That’s it. Once we now delete the new weapon in our profile from the database first then do they once again which have Postman, we get the entire profile straight back – into related firearm.
Leave a Reply
Want to join the discussion?Feel free to contribute!