Skip to content

Curso C# - Entity Framework #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions School/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
namespace School;

public class SchoolMain
{
public static void Main(string[] args)
{
SchoolContext database = new SchoolContext();
database.Database.EnsureDeleted();
database.Database.EnsureCreated();

database.Teachers.Add(new Teacher { Name = "Rosana" });
database.Teachers.Add(new Teacher { Name = "Pedro" });
database.SaveChanges();

StartProgram:

Console.WriteLine("Menu:");
Console.WriteLine("1 - Visualizar dados");
Console.WriteLine("2 - Inserir dados");
Console.WriteLine("3 - Atualizar dados");
Console.WriteLine("4 - Remover dados");
Console.WriteLine("5 - Visualizar pessoas educadoras");

int option = int.Parse(Console.ReadLine()!);

switch(option) {
case 1:
SelectStudents(database);
goto StartProgram;
case 2:
InsertStudents(database);
goto StartProgram;
case 3:
UpdateStudent(database);
goto StartProgram;
case 4:
DeleteStudent(database);
goto StartProgram;
case 5:
SelectTeachers(database);
goto StartProgram;

}


}

public static void SelectStudents( SchoolContext database)
{
Console.Clear();
foreach(Student student in database.Students)
{
Console.WriteLine($"{student.Id} - {student.Name} - {student.Course} - Pessoa Educadora: {student.Teacher.Name}");
}
}


public static void SelectTeachers( SchoolContext database)
{
Console.Clear();
foreach(Teacher teacher in database.Teachers)
{
Console.WriteLine($"{teacher.Id} - {teacher.Name}");
Console.WriteLine("Pessoas estudante");
foreach(Student student in teacher.Students)
{
Console.WriteLine(student.Name);
}
Console.WriteLine("");
}
}

public static void InsertStudents( SchoolContext database)
{
Console.Clear();
Console.WriteLine("Digite o nome da pessoa estudante");
string name = Console.ReadLine()!;

Console.WriteLine("Digite o curso");
string course = Console.ReadLine()!;

Console.WriteLine("Digite o id da pessoa educadora");
int teacherId = int.Parse(Console.ReadLine());

Student newStudent = new Student
{
Name = name,
Course = course,
TeacherId = teacherId
};
database.Students.Add(newStudent);
database.SaveChanges();
}

public static void UpdateStudent(SchoolContext database)
{
Console.Clear();
Console.WriteLine("Informe o Id da Pessoa Estudante");
int id = int.Parse(Console.ReadLine());

Console.WriteLine("Digite o nome da pessoa estudante");
string name = Console.ReadLine()!;

Console.WriteLine("Digite o curso");
string course = Console.ReadLine()!;

Console.WriteLine("Digite o id da pessoa educadora");
int teacherId = int.Parse(Console.ReadLine());

Student existingStudent = database.Students.Find(id);
existingStudent.Name = name;
existingStudent.Course = course;
existingStudent.TeacherId = teacherId;
database.Students.Update(existingStudent);
database.SaveChanges();


}

public static void DeleteStudent( SchoolContext database)
{
Console.Clear();
Console.WriteLine("Informe o Id da Pessoa Estudante");
int id = int.Parse(Console.ReadLine());

Student existingStudent = database.Students.Find(id);
database.Students.Remove(existingStudent);
database.SaveChanges();
}

}
19 changes: 19 additions & 0 deletions School/School.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions School/SchoolContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace School;
using Microsoft.EntityFrameworkCore;

public class SchoolContext : DbContext
{
public SchoolContext(DbContextOptions options) : base(options) {}
public SchoolContext() {}

public DbSet<Student> Students{ get; set; } = null!;
public DbSet<Teacher> Teachers { get; set; } = null!;

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
var connectionString = "Server=localhost;Database=School;User=SA;Password=SqlServer123!;TrustServerCertificate=True";
optionsBuilder.UseSqlServer(connectionString);
}
}

}
14 changes: 14 additions & 0 deletions School/Student.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace School;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
[Key]
public int Id { get; set;}
public string? Name { get; set; }
public string? Course { get; set;}
[ForeignKey("TeacherId")]
public int TeacherId { get; set; }
public Teacher? Teacher { get; set; }
}
10 changes: 10 additions & 0 deletions School/Teacher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace School;
using System.ComponentModel.DataAnnotations;

public class Teacher
{
[Key]
public int Id { get; set; }
public string? Name { get; set; }
public ICollection<Student>? Students { get; set; }
}
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'
services:
db:
image: mcr.microsoft.com/azure-sql-edge:latest
container_name: my_sql_server
ports:
- 1433:1433
environment:
- ACCEPT_EULA=Y
- MSSQL_SA_PASSWORD=SqlServer123!
- MSSQL_PID=Developer
25 changes: 25 additions & 0 deletions sqlserver.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "School", "School\School.csproj", "{4B8AD814-8738-48B0-AECF-3701278325BD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4B8AD814-8738-48B0-AECF-3701278325BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B8AD814-8738-48B0-AECF-3701278325BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B8AD814-8738-48B0-AECF-3701278325BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B8AD814-8738-48B0-AECF-3701278325BD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A3FAFF39-CF24-4D86-9E01-106628DDC79F}
EndGlobalSection
EndGlobal