-1
How are you, my dear? Quiet? I will summarize my project to assist in understanding: I am creating a CRUD to assist me in some of the company’s tasks. I have 3 areas I use (System, Database and Systemdatabase). In the area System I register the systems I use in the company, already in the Database I register all databases that my systems can use and on Systemdatabase the systems are stored together with their active databases. I am using Angular in frontend and Webapi with ASP.NET and EF CORE in backend.
In my form I inform what will be the system I will tell the banks that will be active for it, as a system can communicate with several databases, I have chosen to use a "Multiple select" to list all the banks I can add, see below:
The problem is that I don’t know exactly how to put this data in the table, and the way I did I’m not getting the return I wanted, I did a lot of research on it but there is not much content addressing this subject over the internet. I’ve read language documentation, framework documentation, I asked a colleague for help, I even searched on an Indian site and I couldn’t find much.
My logic was as follows: I thought about creating an array to store the Id of each selected database and when saving (running the POST) it runs a for
which will do a check through an index and if there is data in the array, go adding in the database one by one
To do this, I went to my Model and created a property that is an integer array called Iddatabases:
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace ITControl.Models
{
public partial class SystemDatabase
{
public int IdSystemDatabase { get; set; }
public int IdSystem { get; set; }
public int IdDatabase { get; set; }
[NotMapped]
public int[] IdDatabases { get; set; } // <============ AQUI
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public string UserInserted { get; set; }
public string UserUpdated { get; set; }
public virtual System System { get; set; }
public virtual DataBase DataBase { get; set; }
}
}
And I passed this logic into my controller on [HttpPost]
[HttpPost]
public async Task<ActionResult<SystemDatabase>> PostSystemDatabase(SystemDatabase systemDatabase)
{
systemDatabase.CreatedAt = DateTime.Now;
systemDatabase.UserInserted = (HttpContext.User.Identity as ClaimsIdentity).Name;
for (int i = 0; i < systemDatabase.IdDatabases.Count(); i++)
{
systemDatabase.IdDatabase = systemDatabase.IdDatabases[i];
_context.SystemDatabase.Add(systemDatabase);
}
await _context.SaveChangesAsync();
return CreatedAtAction("GetSystemDatabase", new { id = systemDatabase.IdSystemDatabase }, systemDatabase);
}
The result I was hoping was that he would add one by one he would return something more or less to me in this footprint at the bank:
But in reality he doesn’t add one by one, he’s just playing the last Id on the bench:
I went to the browser developer tools, looked at the response he was giving me on the network when doing the Post and he returned me the following preview:
{IdSystemDatabase: 40, IdSystem: 3, IdDatabase: 13, IdDatabases: [1, 13],…}
CreatedAt: "2020-08-25T08:54:13.5748194-03:00"
DataBase: null
IdDatabase: 13
IdDatabases: [1, 13]
0: 1
1: 13
IdSystem: 3
IdSystemDatabase: 40
System: null
UpdatedAt: null
UserInserted: "OliveiraRBru"
UserUpdated: null
He’s getting the Ids on IdDatabases
but it is not registering one by one, what can I be doing wrong?? To really lost, I believe very much that the error is within mine For
face by the size of your question it seems to be specified from your system, try to ask a more concise and specific question of a problem
– Eduardo Vargas