form1.cs
using System;
using System.Windows.Forms;
namespace WinformAboutEF
{
public partial class create : Form
{
public create()
{
InitializeComponent();
}
private void create_Load(object sender, EventArgs e)
{
}
private void CreateButton_Click(object sender, EventArgs e)
{
CreateWin form = new CreateWin();
form.Show();
}
private void AlterButton_Click(object sender, EventArgs e)
{
AlterWin form = new AlterWin();
form.Show();
}
private void DeleteButton_Click(object sender, EventArgs e)
{
DeleteWin form = new DeleteWin();
form.Show();
}
private void SearchButton_Click(object sender, EventArgs e)
{
FindWin form = new FindWin();
form.Show();
}
}
}
CreateWin.cs
using System;
using System.Windows.Forms;
namespace WinformAboutEF
{
public partial class CreateWin : Form
{
public CreateWin()
{
InitializeComponent();
}
private void searchnow_Click(object sender, EventArgs e)
{
if (textBox1.Text != null)
{
using (var db = new Model1())
{
var s = new Student
{
Id = int.Parse(textBox1.Text),
Name = textBox2.Text,
Age = textBox3.Text
};
db.Student.Add(s);
db.SaveChanges();
textBox4.Text = "添加成功!";
}
}
else
{
textBox4.Text = "添加失败!";
}
}
}
}
DeleteWin.cs
using System;
using System.Linq;
using System.Windows.Forms;
namespace WinformAboutEF
{
public partial class DeleteWin : Form
{
public DeleteWin()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (var db = new Model1())
{
string m = textBox1.Text;
int n = int.Parse(m);
var student = db.Student.FirstOrDefault(o => o.Id == n);
db.Student.Remove(student);
db.SaveChanges();
}
}
}
}
FindWin.cs
using System;
using System.Linq;
using System.Windows.Forms;
namespace WinformAboutEF
{
public partial class FindWin : Form
{
public FindWin()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (var db = new Model1())
{
var i = IdBox.Text;
int n = int.Parse(i);
var result = db.Student.FirstOrDefault(m => m.Id == n);
textBox1.Text = result.Name;
textBox2.Text = result.Age;
}
}
}
}