First you need to choose a database to work with. For C# there are several options, with Microsoft SQL Server the most complete option to work with C#.
To persist your data, the recommended is to use a ORM (Object Relational Mapper, relational object mapper). There are also several options, the most complete being the Entity Framework, that creates and modifies the database for you according to your code, makes lazy and anticipated load of aggregated data, takes care of some validations and more resources.
In the Entity Framework, your Player
gets like this:
public class Player
{
[Key]
public int PlayerId { get; set; }
public int? TeamId { get; set; } // O jogador pode ou não ter um time
public string Name { get; set; }
public virtual Team Team { get; set; }
}
And your Team
:
public class Team
{
[Key]
public int TeamId { get; set; }
public string Name { get; set; }
public virtual ICollection<Player> PlayersInThisTeam { get; set; }
}
Inserting a team:
public SeuProjetoContext context = new SeuProjetoContext();
context.Teams.Add(new Team {
Name = "Time"
});
context.SaveChanges();
Inserting a player:
public SeuProjetoContext context = new SeuProjetoContext();
context.Players.Add(new Player {
Name = "Jogador"
});
context.SaveChanges();
Updating a player:
public SeuProjetoContext context = new SeuProjetoContext();
var player = context.Players.FirstOrDefault(p => p.Name == "Jogador");
if (player != null)
{
player.Name = "Fulano";
context.Entry(player).State = EntityState.Modified;
context.SaveChanges();
}
Excluding a player
public SeuProjetoContext context = new SeuProjetoContext();
var player = context.Players.FirstOrDefault(p => p.Name == "Jogador");
if (player != null)
{
context.Players.Remove(player);
context.SaveChanges();
}
Selecting all the players:
public SeuProjetoContext context = new SeuProjetoContext();
var allPlayers = context.Players.ToList();
Associating a team to a player
public SeuProjetoContext context = new SeuProjetoContext();
var player = context.Players.FirstOrDefault(p => p.Name == "Fulano");
if (player != null)
{
var timeJogador = context.Teams.FirstOfDefault(t => t.Name == "Time");
if (timeJogador != null)
{
player.Time = timeJogador;
context.Entry(player).State = EntityState.Modified;
context.SaveChanges();
}
}
Selecting a team and all its players (advance charge)
public SeuProjetoContext context = new SeuProjetoContext();
var timeComJogadores = context.Teams
.Include(t => t.PlayersInThisTeam)
.FirstOrDefault(t => t.Name == "Time");
// timeJogadores.PlayersInThisTeam virá com os jogadores populados.
Selecting a team and all its players (lazy load)
public SeuProjetoContext context = new SeuProjetoContext();
var timeComJogadores = context.Teams
.FirstOrDefault(t => t.Name == "Time");
var jogadores = timeComJogadores.PlayersInThisTeam.ToList(); // Aqui forço uma segunda consulta apenas com os jogadores.
I’ll set an example for you, as soon as I finish being stationed here
– Pablo Tondolo de Vargas
I found the question a little broad, because answers can come up with "n" different shapes, using "n" different frameworks, etc.. And the answer won’t always have everything you need. It would be more interesting to have more specific information, such as the ORM and database used, what you have already done, if you are giving some error, etc... but it is only a suggestion of improvement ok
– Renan
He doesn’t have a framework specific. The idea of the question is to have just where to start. It is broad, but not at the level that justifies a closure.
– Leonel Sanches da Silva
My question was really a little vague. I’m using Xamarin.Forms to create a mobile app, and I intend to store the data in a Microsoft Azure SQL database
– Lucas Pereira