diff --git a/Cloud/ApplicationContext.cs b/Cloud/ApplicationContext.cs new file mode 100644 index 0000000..e91cc00 --- /dev/null +++ b/Cloud/ApplicationContext.cs @@ -0,0 +1,29 @@ +using Cloud.Models; +using Microsoft.EntityFrameworkCore; + +namespace Cloud; +public class ApplicationContext : DbContext +{ + public DbSet Users { get; set; } + + public ApplicationContext(DbContextOptions options) + : base(options) + { + } + + public ApplicationContext() + : base(new DbContextOptionsBuilder() + .UseNpgsql("Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345") + .Options) + { + Database.EnsureCreated(); + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseNpgsql("Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345"); + } + } +} diff --git a/Cloud/Cloud.csproj b/Cloud/Cloud.csproj index 2c33b1c..d9f0064 100644 --- a/Cloud/Cloud.csproj +++ b/Cloud/Cloud.csproj @@ -7,6 +7,12 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + diff --git a/Cloud/Migrations/20241027220558_CreateUsersTable.Designer.cs b/Cloud/Migrations/20241027220558_CreateUsersTable.Designer.cs new file mode 100644 index 0000000..b021b5d --- /dev/null +++ b/Cloud/Migrations/20241027220558_CreateUsersTable.Designer.cs @@ -0,0 +1,53 @@ +// +using Cloud; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Cloud.Migrations +{ + [DbContext(typeof(ApplicationContext))] + [Migration("20241027220558_CreateUsersTable")] + partial class CreateUsersTable + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.14") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Cloud.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Cloud/Migrations/20241027220558_CreateUsersTable.cs b/Cloud/Migrations/20241027220558_CreateUsersTable.cs new file mode 100644 index 0000000..65e5500 --- /dev/null +++ b/Cloud/Migrations/20241027220558_CreateUsersTable.cs @@ -0,0 +1,34 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Cloud.Migrations +{ + public partial class CreateUsersTable : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false), + Email = table.Column(type: "text", nullable: false), + Password = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/Cloud/Migrations/ApplicationContextModelSnapshot.cs b/Cloud/Migrations/ApplicationContextModelSnapshot.cs new file mode 100644 index 0000000..fc15a07 --- /dev/null +++ b/Cloud/Migrations/ApplicationContextModelSnapshot.cs @@ -0,0 +1,51 @@ +// +using Cloud; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Cloud.Migrations +{ + [DbContext(typeof(ApplicationContext))] + partial class ApplicationContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.14") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Cloud.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Cloud/Models/User.cs b/Cloud/Models/User.cs new file mode 100644 index 0000000..27c8ee4 --- /dev/null +++ b/Cloud/Models/User.cs @@ -0,0 +1,11 @@ +namespace Cloud.Models; +public class User +{ + public int Id { get; set; } + + public string Name { get; set; } + + public string Email { get; set; } + + public string Password { get; set; } +} \ No newline at end of file diff --git a/Cloud/Program.cs b/Cloud/Program.cs index 48863a6..2b9379b 100644 --- a/Cloud/Program.cs +++ b/Cloud/Program.cs @@ -1,7 +1,13 @@ +using Cloud; +using Microsoft.EntityFrameworkCore; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. +builder.Services.AddDbContext(options => + options.UseNpgsql("Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345")); + builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer();