diff --git a/DataAccess/DataAccess.csproj b/DataAccess/DataAccess.csproj new file mode 100644 index 0000000..b60640c --- /dev/null +++ b/DataAccess/DataAccess.csproj @@ -0,0 +1,19 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/DataAccess/Entities/Project.cs b/DataAccess/Entities/Project.cs new file mode 100644 index 0000000..f835c05 --- /dev/null +++ b/DataAccess/Entities/Project.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; +namespace DataAccess{ + public class Project{ + public int Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } + + + + + } +} \ No newline at end of file diff --git a/DataAccess/Entities/User.cs b/DataAccess/Entities/User.cs new file mode 100644 index 0000000..4caa307 --- /dev/null +++ b/DataAccess/Entities/User.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; +namespace DataAccess{ + public class User{ + public int Id { get; set; } + public string Name { get; set; } + + public string LastName { get; set; } + + public string Email { get; set; } + + + } +} \ No newline at end of file diff --git a/DataAccess/Entities/WorkEntry.cs b/DataAccess/Entities/WorkEntry.cs new file mode 100644 index 0000000..33091c0 --- /dev/null +++ b/DataAccess/Entities/WorkEntry.cs @@ -0,0 +1,16 @@ +using Microsoft.EntityFrameworkCore; +namespace DataAccess{ + + + public class WorkEntry{ + public int Id { get; set; } + + public User User { get; set; } + + public Project Project { get; set; } + + public double Hours { get; set; } + + + } +} \ No newline at end of file diff --git a/DataAccess/Migrations/20231101120912_init.Designer.cs b/DataAccess/Migrations/20231101120912_init.Designer.cs new file mode 100644 index 0000000..aaaa938 --- /dev/null +++ b/DataAccess/Migrations/20231101120912_init.Designer.cs @@ -0,0 +1,120 @@ +// +using DataAccess; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DataAccess.Migrations +{ + [DbContext(typeof(ProjectDbContext))] + [Migration("20231101120912_init")] + partial class init + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.11") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DataAccess.Project", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Projects"); + }); + + modelBuilder.Entity("DataAccess.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("DataAccess.WorkEntry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Hours") + .HasColumnType("float"); + + b.Property("ProjectId") + .HasColumnType("int"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProjectId"); + + b.HasIndex("UserId"); + + b.ToTable("workEntries"); + }); + + modelBuilder.Entity("DataAccess.WorkEntry", b => + { + b.HasOne("DataAccess.Project", "Project") + .WithMany() + .HasForeignKey("ProjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DataAccess.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Project"); + + b.Navigation("User"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DataAccess/Migrations/20231101120912_init.cs b/DataAccess/Migrations/20231101120912_init.cs new file mode 100644 index 0000000..23e09ed --- /dev/null +++ b/DataAccess/Migrations/20231101120912_init.cs @@ -0,0 +1,93 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DataAccess.Migrations +{ + /// + public partial class init : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Projects", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Projects", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + LastName = table.Column(type: "nvarchar(max)", nullable: false), + Email = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "workEntries", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UserId = table.Column(type: "int", nullable: false), + ProjectId = table.Column(type: "int", nullable: false), + Hours = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_workEntries", x => x.Id); + table.ForeignKey( + name: "FK_workEntries_Projects_ProjectId", + column: x => x.ProjectId, + principalTable: "Projects", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_workEntries_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_workEntries_ProjectId", + table: "workEntries", + column: "ProjectId"); + + migrationBuilder.CreateIndex( + name: "IX_workEntries_UserId", + table: "workEntries", + column: "UserId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "workEntries"); + + migrationBuilder.DropTable( + name: "Projects"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/DataAccess/Migrations/ProjectDbContextModelSnapshot.cs b/DataAccess/Migrations/ProjectDbContextModelSnapshot.cs new file mode 100644 index 0000000..a5cf716 --- /dev/null +++ b/DataAccess/Migrations/ProjectDbContextModelSnapshot.cs @@ -0,0 +1,117 @@ +// +using DataAccess; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DataAccess.Migrations +{ + [DbContext(typeof(ProjectDbContext))] + partial class ProjectDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.11") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DataAccess.Project", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Projects"); + }); + + modelBuilder.Entity("DataAccess.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("DataAccess.WorkEntry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Hours") + .HasColumnType("float"); + + b.Property("ProjectId") + .HasColumnType("int"); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ProjectId"); + + b.HasIndex("UserId"); + + b.ToTable("workEntries"); + }); + + modelBuilder.Entity("DataAccess.WorkEntry", b => + { + b.HasOne("DataAccess.Project", "Project") + .WithMany() + .HasForeignKey("ProjectId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DataAccess.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Project"); + + b.Navigation("User"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DataAccess/Program.cs b/DataAccess/Program.cs new file mode 100644 index 0000000..8f89152 --- /dev/null +++ b/DataAccess/Program.cs @@ -0,0 +1,88 @@ +// See https://aka.ms/new-console-template for more information +using DataAccess; + +Console.WriteLine("Hello, World!"); + +using (var context = new ProjectDbContext()){ + var user = new User(){ + Name = "Juan", + LastName = "Perez", + Email = "JuanPerez@gmail.com", + }; + context.Users.Add(user); + context.SaveChanges(); + + var user2 = new User(){ + Name = "Pedro", + LastName = "Perez", + Email = "PedroPerez@gmail.com", + }; + + context.Users.Add(user2); + context.SaveChanges(); + + var project = new Project(){ + Name = "Proyecto 1", + Description = "Proyecto de prueba", + }; + + context.Projects.Add(project); + context.SaveChanges(); + + var project2 = new Project(){ + Name = "Proyecto 2", + Description = "Proyecto de prueba 2", + }; + + context.Projects.Add(project2); + context.SaveChanges(); + + var workEntry = new WorkEntry(){ + User = user, + Project = project, + Hours = 5, + }; + + context.Attach(user); + context.Attach(project); + context.workEntries.Add(workEntry); + context.SaveChanges(); + + var workEntry2 = new WorkEntry(){ + User = user, + Project = project2, + Hours = 10, + }; + + context.Attach(user); + context.Attach(project2); + context.workEntries.Add(workEntry2); + context.SaveChanges(); + + var workEntry3 = new WorkEntry(){ + User = user2, + Project = project, + Hours = 15, + }; + + context.Attach(user2); + context.Attach(project); + context.workEntries.Add(workEntry3); + context.SaveChanges(); + + var workEntry4 = new WorkEntry(){ + User = user2, + Project = project2, + Hours = 20, + }; + + context.Attach(user2); + context.Attach(project2); + context.workEntries.Add(workEntry4); + context.SaveChanges(); + + + +} + + diff --git a/DataAccess/ProjectDbContext.cs b/DataAccess/ProjectDbContext.cs new file mode 100644 index 0000000..e5ff946 --- /dev/null +++ b/DataAccess/ProjectDbContext.cs @@ -0,0 +1,22 @@ + +using Microsoft.EntityFrameworkCore; +namespace DataAccess +{ + public class ProjectDbContext : DbContext + { + public DbSet Users { get; set; } + public DbSet Projects { get; set; } + + public DbSet workEntries { get; set; } + + + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){ + optionsBuilder.UseSqlServer("Server=DESKTOP-O4959SS;Database=ProjectDB;Integrated Security=True;Trusted_Connection=True;Encrypt=False;"); + } + + + + } + +} \ No newline at end of file