Hi @Anonymous ,
Based on your description, it looks like you want to implement database CRUD via .NET WebApi and entity framework.
In fact, in the official documentation, you can find a simple example of it. Just like this doc (with example): Using Web API 2 with Entity Framework 6 Tutorial Series.
In your case, I think your main problem is that you need to process the request data during the request process, and then do CRUD operations in these two tables separately. For example, you will try to insert a record of player. In the json data you provided, it actually contains the data of two tables, you need to get it in [FromBody] or HttpRequestMessage, and do the necessary processing, like this:
public IHttpActionResult Post([FromBody] players player)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
else
{
//insert new record into database.
db.Players.Add(player);
db.SaveChanges();
foreach (var skill in player.playerSkills)
{
//set the ID of each skill
skill.playerId = player.Id;
db.PlayerSkills.Add(skill);
db.SaveChanges();
}
return Ok(player);
}
}
You need to manually assign the playerId property to each playerSkill object. So it mainly depends on your table structure design and logical processing.
In addition, for authorization token, you can simply refer to this documentation: Authentication and Authorization in ASP.NET Web API.
Best regards,
Xudong Peng
If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.