データベースにSQL Serverを利用する場合の手順はこちらの記事を参照してください。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleEntityFrameworkCoreSqlite
{
public class MyRec
{
public int ID { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public int Price { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Sqlite;
namespace SimpleEntityFrameworkCoreSqlite
{
public class MyContext : DbContext
{
public DbSet<MyRec> Rec { get; set; }
public string DbPath { get; }
public MyContext()
{
DbPath = @"C:\storage\data\mydata.db";
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlite("Data Source=" + DbPath);
}
}
}
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
namespace SimpleEntityFrameworkCoreSqlite
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyContext mc = new MyContext();
RelationalDatabaseCreator rdc = (RelationalDatabaseCreator)mc.Database.GetService<IDatabaseCreator>();
rdc.CreateTables();
textBox1.Text += "テーブルを作成しました。\r\n";
}
private void button2_Click(object sender, EventArgs e)
{
MyContext mc = new MyContext();
mc = new MyContext();
MyRec mr = new MyRec();
mr.Name = "ぺんぎんクッキー";
mr.Category = "焼き菓子";
mr.Price = 240;
mc.Add(mr);
mr = new MyRec();
mr.Name = "らくだキャラメル";
mr.Category = "生菓子";
mr.Price = 85;
mc.Add(mr);
mr = new MyRec();
mr.Name = "しろくまアイス";
mr.Category = "氷菓";
mr.Price = 320;
mc.Add(mr);
mc.SaveChanges();
textBox1.Text += "レコードを挿入しました。\r\n";
}
private void button3_Click(object sender, EventArgs e)
{
MyContext mc = new MyContext();
IEnumerable<MyRec> result = mc.Rec;
foreach (MyRec m in result) {
textBox1.Text += string.Format("{0:d} {1} {2} {3:d}\r\n", m.ID, m.Name, m.Category, m.Price);
}
}
}
}
public class MyRec
{
public int ID { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public int Price { get; set; }
}
public class MyContext : DbContext
{
public DbSet<MyRec> Rec { get; set; }
public string DbPath { get; }
public MyContext()
{
DbPath = @"C:\storage\data\mydata.db";
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlite("Data Source=" + DbPath);
}
}
private void button1_Click(object sender, EventArgs e)
{
MyContext mc = new MyContext();
RelationalDatabaseCreator rdc = (RelationalDatabaseCreator)mc.Database.GetService<IDatabaseCreator>();
rdc.CreateTables();
textBox1.Text += "テーブルを作成しました。\r\n";
}
private void button2_Click(object sender, EventArgs e)
{
MyContext mc = new MyContext();
MyRec mr = new MyRec();
mr.Name = "ぺんぎんクッキー";
mr.Category = "焼き菓子";
mr.Price = 240;
mc.Add(mr);
mr = new MyRec();
mr.Name = "らくだキャラメル";
mr.Category = "生菓子";
mr.Price = 85;
mc.Add(mr);
mr = new MyRec();
mr.Name = "しろくまアイス";
mr.Category = "氷菓";
mr.Price = 320;
mc.Add(mr);
mc.SaveChanges();
textBox1.Text += "レコードを挿入しました。\r\n";
}
private void button3_Click(object sender, EventArgs e)
{
MyContext mc = new MyContext();
IEnumerable<MyRec> result = mc.Rec;
foreach (MyRec m in result) {
textBox1.Text += string.Format("{0:d} {1} {2} {3:d}\r\n", m.ID, m.Name, m.Category, m.Price);
}
}
C:\Storage\data\mydata.db
ファイルが作成されていることを確認できます。