From 4eb79028ddac18b20a6edffe12c4ee475a76d56d Mon Sep 17 00:00:00 2001 From: Santiago Date: Wed, 1 Nov 2023 09:54:14 -0300 Subject: [PATCH] Santiago Trinidad 281306 --- ConsoleApp1/CargaHoras.cs | 11 ++ ConsoleApp1/ConsoleApp1.csproj | 19 +++ .../Migrations/20231101125250_v1.Designer.cs | 143 ++++++++++++++++++ ConsoleApp1/Migrations/20231101125250_v1.cs | 105 +++++++++++++ .../ProyectosDbContextModelSnapshot.cs | 140 +++++++++++++++++ ConsoleApp1/Program.cs | 78 ++++++++++ ConsoleApp1/Proyecto.cs | 7 + ConsoleApp1/Usuario.cs | 8 + ConsoleApp1/dbcontext.cs | 26 ++++ PruebaEntity.sln | 22 +++ 10 files changed, 559 insertions(+) create mode 100644 ConsoleApp1/CargaHoras.cs create mode 100644 ConsoleApp1/ConsoleApp1.csproj create mode 100644 ConsoleApp1/Migrations/20231101125250_v1.Designer.cs create mode 100644 ConsoleApp1/Migrations/20231101125250_v1.cs create mode 100644 ConsoleApp1/Migrations/ProyectosDbContextModelSnapshot.cs create mode 100644 ConsoleApp1/Program.cs create mode 100644 ConsoleApp1/Proyecto.cs create mode 100644 ConsoleApp1/Usuario.cs create mode 100644 ConsoleApp1/dbcontext.cs create mode 100644 PruebaEntity.sln diff --git a/ConsoleApp1/CargaHoras.cs b/ConsoleApp1/CargaHoras.cs new file mode 100644 index 0000000..d76ab63 --- /dev/null +++ b/ConsoleApp1/CargaHoras.cs @@ -0,0 +1,11 @@ +public class CargaHoras +{ + public int CargaHorasId { get; set; } + public int UsuarioId { get; set; } + public virtual Usuario Usuario { get; set; } + public int ProyectoId { get; set; } + public virtual Proyecto Proyecto { get; set; } + public decimal HorasTrabajadas { get; set; } + public string Descripcion { get; set; } + public DateTime Fecha { get; set; } +} \ No newline at end of file diff --git a/ConsoleApp1/ConsoleApp1.csproj b/ConsoleApp1/ConsoleApp1.csproj new file mode 100644 index 0000000..37d6f01 --- /dev/null +++ b/ConsoleApp1/ConsoleApp1.csproj @@ -0,0 +1,19 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/ConsoleApp1/Migrations/20231101125250_v1.Designer.cs b/ConsoleApp1/Migrations/20231101125250_v1.Designer.cs new file mode 100644 index 0000000..7140387 --- /dev/null +++ b/ConsoleApp1/Migrations/20231101125250_v1.Designer.cs @@ -0,0 +1,143 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConsoleApp1.Migrations +{ + [DbContext(typeof(ProyectosDbContext))] + [Migration("20231101125250_v1")] + partial class v1 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("CargaHoras", b => + { + b.Property("CargaHorasId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("CargaHorasId")); + + b.Property("Descripcion") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Fecha") + .HasColumnType("datetime2"); + + b.Property("HorasTrabajadas") + .HasColumnType("decimal(18,2)"); + + b.Property("ProyectoId") + .HasColumnType("int"); + + b.Property("UsuarioId") + .HasColumnType("int"); + + b.HasKey("CargaHorasId"); + + b.HasIndex("ProyectoId"); + + b.HasIndex("UsuarioId"); + + b.ToTable("CargasHoras"); + }); + + modelBuilder.Entity("Proyecto", b => + { + b.Property("ProyectoId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ProyectoId")); + + b.Property("Nombre") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ProyectoId"); + + b.ToTable("Proyectos"); + }); + + modelBuilder.Entity("Usuario", b => + { + b.Property("UsuarioId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("UsuarioId")); + + b.Property("Nombre") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProyectoId") + .HasColumnType("int"); + + b.HasKey("UsuarioId"); + + b.HasIndex("ProyectoId"); + + b.ToTable("Usuarios"); + }); + + modelBuilder.Entity("CargaHoras", b => + { + b.HasOne("Proyecto", "Proyecto") + .WithMany("CargasHoras") + .HasForeignKey("ProyectoId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Usuario", "Usuario") + .WithMany("CargasHoras") + .HasForeignKey("UsuarioId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Proyecto"); + + b.Navigation("Usuario"); + }); + + modelBuilder.Entity("Usuario", b => + { + b.HasOne("Proyecto", "Proyecto") + .WithMany("Usuarios") + .HasForeignKey("ProyectoId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Proyecto"); + }); + + modelBuilder.Entity("Proyecto", b => + { + b.Navigation("CargasHoras"); + + b.Navigation("Usuarios"); + }); + + modelBuilder.Entity("Usuario", b => + { + b.Navigation("CargasHoras"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ConsoleApp1/Migrations/20231101125250_v1.cs b/ConsoleApp1/Migrations/20231101125250_v1.cs new file mode 100644 index 0000000..cc6ea25 --- /dev/null +++ b/ConsoleApp1/Migrations/20231101125250_v1.cs @@ -0,0 +1,105 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConsoleApp1.Migrations +{ + /// + public partial class v1 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Proyectos", + columns: table => new + { + ProyectoId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Nombre = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Proyectos", x => x.ProyectoId); + }); + + migrationBuilder.CreateTable( + name: "Usuarios", + columns: table => new + { + UsuarioId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Nombre = table.Column(type: "nvarchar(max)", nullable: false), + ProyectoId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Usuarios", x => x.UsuarioId); + table.ForeignKey( + name: "FK_Usuarios_Proyectos_ProyectoId", + column: x => x.ProyectoId, + principalTable: "Proyectos", + principalColumn: "ProyectoId", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "CargasHoras", + columns: table => new + { + CargaHorasId = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UsuarioId = table.Column(type: "int", nullable: false), + ProyectoId = table.Column(type: "int", nullable: false), + HorasTrabajadas = table.Column(type: "decimal(18,2)", nullable: false), + Descripcion = table.Column(type: "nvarchar(max)", nullable: false), + Fecha = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CargasHoras", x => x.CargaHorasId); + table.ForeignKey( + name: "FK_CargasHoras_Proyectos_ProyectoId", + column: x => x.ProyectoId, + principalTable: "Proyectos", + principalColumn: "ProyectoId", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_CargasHoras_Usuarios_UsuarioId", + column: x => x.UsuarioId, + principalTable: "Usuarios", + principalColumn: "UsuarioId", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateIndex( + name: "IX_CargasHoras_ProyectoId", + table: "CargasHoras", + column: "ProyectoId"); + + migrationBuilder.CreateIndex( + name: "IX_CargasHoras_UsuarioId", + table: "CargasHoras", + column: "UsuarioId"); + + migrationBuilder.CreateIndex( + name: "IX_Usuarios_ProyectoId", + table: "Usuarios", + column: "ProyectoId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "CargasHoras"); + + migrationBuilder.DropTable( + name: "Usuarios"); + + migrationBuilder.DropTable( + name: "Proyectos"); + } + } +} diff --git a/ConsoleApp1/Migrations/ProyectosDbContextModelSnapshot.cs b/ConsoleApp1/Migrations/ProyectosDbContextModelSnapshot.cs new file mode 100644 index 0000000..80e9dd5 --- /dev/null +++ b/ConsoleApp1/Migrations/ProyectosDbContextModelSnapshot.cs @@ -0,0 +1,140 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConsoleApp1.Migrations +{ + [DbContext(typeof(ProyectosDbContext))] + partial class ProyectosDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("CargaHoras", b => + { + b.Property("CargaHorasId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("CargaHorasId")); + + b.Property("Descripcion") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Fecha") + .HasColumnType("datetime2"); + + b.Property("HorasTrabajadas") + .HasColumnType("decimal(18,2)"); + + b.Property("ProyectoId") + .HasColumnType("int"); + + b.Property("UsuarioId") + .HasColumnType("int"); + + b.HasKey("CargaHorasId"); + + b.HasIndex("ProyectoId"); + + b.HasIndex("UsuarioId"); + + b.ToTable("CargasHoras"); + }); + + modelBuilder.Entity("Proyecto", b => + { + b.Property("ProyectoId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ProyectoId")); + + b.Property("Nombre") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ProyectoId"); + + b.ToTable("Proyectos"); + }); + + modelBuilder.Entity("Usuario", b => + { + b.Property("UsuarioId") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("UsuarioId")); + + b.Property("Nombre") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ProyectoId") + .HasColumnType("int"); + + b.HasKey("UsuarioId"); + + b.HasIndex("ProyectoId"); + + b.ToTable("Usuarios"); + }); + + modelBuilder.Entity("CargaHoras", b => + { + b.HasOne("Proyecto", "Proyecto") + .WithMany("CargasHoras") + .HasForeignKey("ProyectoId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Usuario", "Usuario") + .WithMany("CargasHoras") + .HasForeignKey("UsuarioId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Proyecto"); + + b.Navigation("Usuario"); + }); + + modelBuilder.Entity("Usuario", b => + { + b.HasOne("Proyecto", "Proyecto") + .WithMany("Usuarios") + .HasForeignKey("ProyectoId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Proyecto"); + }); + + modelBuilder.Entity("Proyecto", b => + { + b.Navigation("CargasHoras"); + + b.Navigation("Usuarios"); + }); + + modelBuilder.Entity("Usuario", b => + { + b.Navigation("CargasHoras"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs new file mode 100644 index 0000000..339bbed --- /dev/null +++ b/ConsoleApp1/Program.cs @@ -0,0 +1,78 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); + +using (var context = new ProyectosDbContext()) +{ + var proyecto1 = new Proyecto() + { + Nombre = "Proyecto 1" + }; + + var proyecto2 = new Proyecto() + { + Nombre = "Proyecto 2" + }; + + var proyecto3 = new Proyecto() + { + Nombre = "Proyecto 3" + }; + + context.Proyectos.Attach(proyecto1); + context.Proyectos.Attach(proyecto2); + context.Proyectos.Attach(proyecto3); + + var usuario1 = new Usuario() + { + Nombre = "Usuario 1", + Proyecto = proyecto1 + }; + + var usuario2 = new Usuario() + { + Nombre = "Usuario 2", + Proyecto = proyecto2 + }; + + var usuario3 = new Usuario() + { + Nombre = "Usuario 3", + Proyecto = proyecto3 + }; + + context.Usuarios.Attach(usuario1); + context.Usuarios.Attach(usuario2); + context.Usuarios.Attach(usuario3); + + context.Proyectos.Add(proyecto1); + context.Proyectos.Add(proyecto2); + context.Proyectos.Add(proyecto3); + + context.Usuarios.Add(usuario1); + context.Usuarios.Add(usuario2); + context.Usuarios.Add(usuario3); + + context.CargasHoras.AddRange( + new CargaHoras() + { + Usuario = usuario1, + Proyecto = proyecto1, + HorasTrabajadas = 5, + Descripcion = "Actividad 1", + Fecha = DateTime.Now + }, + new CargaHoras() + { + Usuario = usuario1, + Proyecto = proyecto1, + HorasTrabajadas = 3, + Descripcion = "Actividad 2", + Fecha = DateTime.Now + } + ); + + + context.SaveChanges(); +} + + diff --git a/ConsoleApp1/Proyecto.cs b/ConsoleApp1/Proyecto.cs new file mode 100644 index 0000000..daf1382 --- /dev/null +++ b/ConsoleApp1/Proyecto.cs @@ -0,0 +1,7 @@ +public class Proyecto +{ + public int ProyectoId { get; set; } + public string Nombre { get; set; } + public virtual ICollection Usuarios { get; set; } + public virtual ICollection CargasHoras { get; set; } +} \ No newline at end of file diff --git a/ConsoleApp1/Usuario.cs b/ConsoleApp1/Usuario.cs new file mode 100644 index 0000000..b5494e4 --- /dev/null +++ b/ConsoleApp1/Usuario.cs @@ -0,0 +1,8 @@ +public class Usuario +{ + public int UsuarioId { get; set; } + public string Nombre { get; set; } + public int ProyectoId { get; set; } + public virtual Proyecto Proyecto { get; set; } + public virtual ICollection CargasHoras { get; set; } +} \ No newline at end of file diff --git a/ConsoleApp1/dbcontext.cs b/ConsoleApp1/dbcontext.cs new file mode 100644 index 0000000..45911ec --- /dev/null +++ b/ConsoleApp1/dbcontext.cs @@ -0,0 +1,26 @@ +using Microsoft.EntityFrameworkCore; + +public class ProyectosDbContext : DbContext +{ + public DbSet Proyectos { get; set; } + public DbSet Usuarios { get; set; } + public DbSet CargasHoras { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=PruebaEntity;Integrated Security=True;Trusted_Connection=True;Encrypt=False"); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity() + .HasOne(u => u.Proyecto) + .WithMany(p => p.Usuarios) + .OnDelete(DeleteBehavior.Restrict); + modelBuilder.Entity() + .HasOne(u => u.Usuario) + .WithMany(p => p.CargasHoras) + .OnDelete(DeleteBehavior.Restrict); + } +} \ No newline at end of file diff --git a/PruebaEntity.sln b/PruebaEntity.sln new file mode 100644 index 0000000..a4c5a5f --- /dev/null +++ b/PruebaEntity.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{8494DA72-623D-4699-BE24-8C7F4A90C8CC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8494DA72-623D-4699-BE24-8C7F4A90C8CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8494DA72-623D-4699-BE24-8C7F4A90C8CC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8494DA72-623D-4699-BE24-8C7F4A90C8CC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8494DA72-623D-4699-BE24-8C7F4A90C8CC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal