entityframework with mysql and basic controller

main
Peace 11 months ago
parent 8dc81a7971
commit 486d2f7f8b
  1. 11
      AspNetCoreApi/AspNetCoreApi.csproj
  2. 26
      AspNetCoreApi/Controllers/ProductController.cs
  3. 15
      AspNetCoreApi/DbContexts/AppDbContext.cs
  4. 49
      AspNetCoreApi/Migrations/20240614040515_ef_inital_commit.Designer.cs
  5. 41
      AspNetCoreApi/Migrations/20240614040515_ef_inital_commit.cs
  6. 46
      AspNetCoreApi/Migrations/AppDbContextModelSnapshot.cs
  7. 9
      AspNetCoreApi/Models/Product.cs
  8. 42
      AspNetCoreApi/Program.cs
  9. 4
      README.md

@ -7,6 +7,17 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

@ -0,0 +1,26 @@
using AspNetCoreApi.DbContexts;
using AspNetCoreApi.Models;
using Microsoft.AspNetCore.Mvc;
namespace AspNetCoreApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class ProductController : ControllerBase
{
private readonly ILogger<ProductController> _logger;
private readonly AppDbContext _dbContext;
public ProductController(ILogger<ProductController> logger, AppDbContext dbContext)
{
_logger = logger;
_dbContext = dbContext;
}
[HttpGet(Name = "GetProduct")]
public IEnumerable<Product> Get()
{
return _dbContext.Products.ToList();
}
}
}

@ -0,0 +1,15 @@
using AspNetCoreApi.Models;
using Microsoft.EntityFrameworkCore;
namespace AspNetCoreApi.DbContexts
{
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
}
}

@ -0,0 +1,49 @@
// <auto-generated />
using AspNetCoreApi.DbContexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace AspNetCoreApi.Migrations
{
[DbContext(typeof(AppDbContext))]
[Migration("20240614040515_ef_inital_commit")]
partial class ef_inital_commit
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.6")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
modelBuilder.Entity("AspNetCoreApi.Models.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<decimal>("Price")
.HasColumnType("decimal(65,30)");
b.HasKey("Id");
b.ToTable("Products");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,41 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AspNetCoreApi.Migrations
{
/// <inheritdoc />
public partial class ef_inital_commit : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Price = table.Column<decimal>(type: "decimal(65,30)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Products", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Products");
}
}
}

@ -0,0 +1,46 @@
// <auto-generated />
using AspNetCoreApi.DbContexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace AspNetCoreApi.Migrations
{
[DbContext(typeof(AppDbContext))]
partial class AppDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.6")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
modelBuilder.Entity("AspNetCoreApi.Models.Product", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("longtext");
b.Property<decimal>("Price")
.HasColumnType("decimal(65,30)");
b.HasKey("Id");
b.ToTable("Products");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,9 @@
namespace AspNetCoreApi.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}

@ -1,5 +1,8 @@
using AspNetCoreApi.DbContexts;
using AspNetCoreApi.Middlewares;
using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore.MySql;
namespace AspNetCoreApi
{
@ -9,6 +12,15 @@ namespace AspNetCoreApi
{
var builder = WebApplication.CreateBuilder(args);
// Add EF context
var connString = "server=peacecloud.synology.me;port=23306;database=Study;uid=study;password=Study1234!";
var dbServerVersion = new MySqlServerVersion(new Version(10, 3, 32));
builder.Services.AddDbContext<AppDbContext>(
options => options
.UseMySql(connString, dbServerVersion));
// Add services to the container.
builder.Services.AddControllers();
@ -34,20 +46,22 @@ namespace AspNetCoreApi
app.UseAuthorization();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/routingtest", async context =>
{
await context.Response.WriteAsync("Hello, World!");
});
endpoints.MapGet("/routingtest/hello/{name}", async context =>
{
var name = context.Request.RouteValues["name"];
await context.Response.WriteAsync($"Hello, {name}!");
});
});
//// Add Routing
//app.UseRouting();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapGet("/routingtest", async context =>
// {
// await context.Response.WriteAsync("Hello, World!");
// });
// endpoints.MapGet("/routingtest/hello/{name}", async context =>
// {
// var name = context.Request.RouteValues["name"];
// await context.Response.WriteAsync($"Hello, {name}!");
// });
//});
app.MapControllers();

@ -5,4 +5,6 @@
- API Server
- Middleware
- Routing
- Routing
- EntityFramework
- Controller
Loading…
Cancel
Save