ef migration

main
syneffort 2 years ago
parent e071c1a01d
commit 62e77721a9
  1. 58
      AspNetFrameworkMVC/AspNetCoreMVC/Controllers/UserController.cs
  2. 18
      AspNetFrameworkMVC/AspNetCoreMVC/DataContext/ModelDbContext.cs
  3. 71
      AspNetFrameworkMVC/AspNetCoreMVC/Migrations/20230519075640_first-mig.Designer.cs
  4. 49
      AspNetFrameworkMVC/AspNetCoreMVC/Migrations/20230519075640_first-mig.cs
  5. 69
      AspNetFrameworkMVC/AspNetCoreMVC/Migrations/ModelDbContextModelSnapshot.cs
  6. 11
      AspNetFrameworkMVC/AspNetCoreMVC/Models/Person.cs
  7. 2
      AspNetFrameworkMVC/AspNetCoreMVC/Models/User.cs
  8. 5
      AspNetFrameworkMVC/AspNetCoreMVC/appsettings.json

@ -1,4 +1,5 @@
using AspNetCoreMVC.Models; using AspNetCoreMVC.DataContext;
using AspNetCoreMVC.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Diagnostics; using System.Diagnostics;
@ -7,62 +8,19 @@ namespace AspNetCoreMVC.Controllers
[Route("api/user")] // 컨트롤러 전체 라우터 설정 [Route("api/user")] // 컨트롤러 전체 라우터 설정
public class UserController : Controller public class UserController : Controller
{ {
private static List<User> _users;
public UserController() : base()
{
if (_users != null)
return;
_users = new List<User>();
_users.Add(new User
{
Id = 1,
Name = "James",
Phone = "123-4567-8910",
Address = "Somewhere over the rainbow1"
});
_users.Add(new User
{
Id = 2,
Name = "Carl",
Phone = "223-4567-8910",
Address = "Somewhere over the rainbow2"
});
_users.Add(new User
{
Id = 3,
Name = "Tyler",
Phone = "323-4567-8910",
Address = "Somewhere over the rainbow3"
});
_users.Add(new User
{
Id = 4,
Name = "Jackson",
Phone = "423-4567-8910",
Address = "Somewhere over the rainbow4"
});
_users.Add(new User
{
Id = 5,
Name = "Philip",
Phone = "523-4567-8910",
Address = "Somewhere over the rainbow5"
});
}
[HttpGet("{id}")] [HttpGet("{id}")]
public JsonResult GetUser(int id) public JsonResult GetUser(int id)
{ {
var user = _users.SingleOrDefault(u => u.Id == id); var db = new ModelDbContext();
var user = db.Users.SingleOrDefault(u => u.Id == id);
return Json(user); return Json(user);
} }
[HttpGet("v2/{id}")] [HttpGet("v2/{id}")]
public IActionResult GetUser2(int id) public IActionResult GetUser2(int id)
{ {
var user = _users.SingleOrDefault(u => u.Id == id); var db = new ModelDbContext();
var user = db.Users.SingleOrDefault(u => u.Id == id);
if (user == null) if (user == null)
return BadRequest("No data found"); return BadRequest("No data found");
@ -74,10 +32,12 @@ namespace AspNetCoreMVC.Controllers
{ {
try try
{ {
var db = new ModelDbContext();
if (!ModelState.IsValid) if (!ModelState.IsValid)
return Json(false); return Json(false);
_users.Add(user); db.Users.Add(user);
return Json(true); return Json(true);
} }
catch (Exception ex) catch (Exception ex)

@ -0,0 +1,18 @@
using AspNetCoreMVC.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace AspNetCoreMVC.DataContext
{
public class ModelDbContext : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = "server=peacecloud.synology.me,21433;database=study;uid=study;pwd=Study1234";
optionsBuilder.UseSqlServer(connectionString);
}
}
}

@ -0,0 +1,71 @@
// <auto-generated />
using AspNetCoreMVC.DataContext;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace AspNetCoreMVC.Migrations
{
[DbContext(typeof(ModelDbContext))]
[Migration("20230519075640_first-mig")]
partial class firstmig
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.16")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("AspNetCoreMVC.Models.Person", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Person");
});
modelBuilder.Entity("AspNetCoreMVC.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<string>("Phone")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("User");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,49 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AspNetCoreMVC.Migrations
{
public partial class firstmig : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Person",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Person", x => x.Id);
});
migrationBuilder.CreateTable(
name: "User",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
Phone = table.Column<string>(type: "nvarchar(max)", nullable: false),
Address = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_User", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Person");
migrationBuilder.DropTable(
name: "User");
}
}
}

@ -0,0 +1,69 @@
// <auto-generated />
using AspNetCoreMVC.DataContext;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace AspNetCoreMVC.Migrations
{
[DbContext(typeof(ModelDbContext))]
partial class ModelDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.16")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("AspNetCoreMVC.Models.Person", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Person");
});
modelBuilder.Entity("AspNetCoreMVC.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<string>("Phone")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("User");
});
#pragma warning restore 612, 618
}
}
}

@ -9,15 +9,4 @@ namespace AspNetCoreMVC.Models
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
} }
public class PersonDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = "server=peacecloud.synology.me,21433;database=study;uid=study;pwd=Study1234";
optionsBuilder.UseSqlServer(connectionString);
}
public DbSet<Person> Persons { get; set; }
}
} }

@ -1,7 +1,9 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AspNetCoreMVC.Models namespace AspNetCoreMVC.Models
{ {
[Table("User")]
public class User public class User
{ {
[Required] [Required]

@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "server=peacecloud.synology.me,21433;database=study;uid=study;pwd=Study1234"
}
} }

Loading…
Cancel
Save