티스토리 뷰
Web.config
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyContext" connectionString="Server=localhost;Database=MyDatabase;User ID=sa;Password=your_password" providerName="Microsoft.EntityFrameworkCore.SqlServer" />
</connectionStrings>
</configuration>
DbContext
using Microsoft.EntityFrameworkCore;
namespace MyApp
{
public class MyContext :DbContext
{
public MyContext(DbContextOptions options) : base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(Configuration["connectionStrings:MyContext"]);
}
}
}
OR
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace MyApp
{
public class MyContext : DbContext
{
public MyContext(DbContextOptions options) : base(options)
{
}
public DbSet<User> Users { get; set; }
publicDbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseSqlServer(configuration["connectionStrings:MyContext"]);
}
}
}
using Microsoft.EntityFrameworkCore;
namespace MyApp
{
public class MyContext : DbContext
{
public MyContext(DbContextOptions options) : base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().HasKey(u => u.Id);
modelBuilder.Entity<User>().Property(u => u.Name).IsRequired();
modelBuilder.Entity<User>().Property(u => u.Email).IsRequired();
modelBuilder.Entity<Product>().HasKey(p => p.Id);
modelBuilder.Entity<Product>().Property(p => p.Name).IsRequired();
modelBuilder.Entity<Product>().Property(p => p.Price).IsRequired();
}
}
}
using MyApp;
namespace MyApp
{
public class Program
{
public static void Main(string[] args)
{
using (var context = new MyContext())
{
var user = new User { Name = "John Doe", Email = "test000@example.com" };
var product = new Product { Name = "Apple iPhone 12", Price = 1000 };
context.Users.Add(user);
context.Products.Add(product);
context.SaveChanges();
}
}
}
}
modelBuilder.Entity().HasDbFunction(ef => ef.Function("MyProcedure"));
using (var context = new MyContext())
{
var result = context.MyProcedure(param1, param2, param3);
}
OR
using (var context = new MyContext())
{
var result = context.MyProcedure();
var value = result.FirstOrDefault();
}
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 아이폰
- 제품 등록
- 상품 등록
- 문자 자르기
- 앱 스토어
- nl2br
- mssql
- system.io
- App Store
- MainActor
- FileUpload
- 진수 변환
- 특정 문자를 기준으로 자르기
- In App Purchase
- hyper-v
- await
- async
- Task
- html tag
- System.Diagnostics
- 공백제거
- 명령어
- trim
- .submit()
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함