티스토리 뷰
728x90
반응형
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();
}
728x90
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- error-java
- 상품 등록
- java.sql
- MainActor
- .submit()
- jsp 오픈 소스
- 메이븐(maven)
- 인텔리제이(intellij)
- 문자 자르기
- 제품 등록
- 진수 변환
- 특정 문자를 기준으로 자르기
- java-개발 환경 설정하기
- 표현 언어(expression language)
- 스프링 프레임워크(spring framework)
- system.io
- java 키워드 정리
- jstl(java standard tag library)
- 람다식(lambda expression)
- 스프링 프레임워크(spring framewordk)
- jstl(java standard tag library)-core
- 스프링 시큐리티(spring security)-http basic 인증
- System.Diagnostics
- In App Purchase
- React
- await
- nl2br
- REST API
- java web-mvc
- 스프링 시큐리티(spring security)
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함