Last active
December 12, 2019 09:35
-
-
Save ouzdev/27ced61631bf40c9cee88b8cab08c49c to your computer and use it in GitHub Desktop.
Entity Framework Basic CRUD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace EntityFramework_demo | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
ProductDal _productDal = new ProductDal(); | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
dgw_items.DataSource = _productDal.GetAll(); | |
} | |
private void btn_AddProduct_Click(object sender, EventArgs e) | |
{ | |
_productDal.Add(new Product | |
{ | |
Name = tbxName.Text, | |
UnitPrice = Convert.ToDecimal(tbxUnitPrice.Text), | |
StockAmount = Convert.ToInt32(tbxStockAmount.Text) | |
}); | |
dgw_items.DataSource = _productDal.GetAll(); | |
MessageBox.Show("Add Product"); | |
} | |
private void btn_Update_Click(object sender, EventArgs e) | |
{ | |
_productDal.Update(new Product | |
{ | |
Id =Convert.ToInt32(dgw_items.CurrentRow.Cells[0].Value), | |
Name = tbx_Name_Update.Text, | |
UnitPrice = Convert.ToDecimal(tbx_UnitPrice_Update.Text), | |
StockAmount = Convert.ToInt32(tbx_StockAmount_Update.Text) | |
}); | |
dgw_items.DataSource = _productDal.GetAll(); | |
MessageBox.Show("Update Product"); | |
} | |
private void btn_delete_product_Click(object sender, EventArgs e) | |
{ | |
_productDal.Delete(new Product | |
{ | |
Id = Convert.ToInt32(dgw_items.CurrentRow.Cells[0].Value), | |
}); | |
dgw_items.DataSource = _productDal.GetAll(); | |
MessageBox.Show("Delete Product"); | |
} | |
private void dgw_items_CellClick(object sender, DataGridViewCellEventArgs e) | |
{ | |
tbx_Name_Update.Text = dgw_items.CurrentRow.Cells[1].Value.ToString(); | |
tbx_UnitPrice_Update.Text = dgw_items.CurrentRow.Cells[2].Value.ToString(); | |
tbx_StockAmount_Update.Text = dgw_items.CurrentRow.Cells[3].Value.ToString(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace EntityFramework_demo | |
{ | |
public class Product | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public decimal UnitPrice { get; set; } | |
public int StockAmount { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Data.Entity; | |
namespace EntityFramework_demo | |
{ | |
public class ProductDal | |
{ | |
public List<Product> GetAll() | |
{ | |
using (ETradeContext context = new ETradeContext()) | |
{ | |
return context.Products.ToList(); | |
} | |
} | |
public void Add(Product product) | |
{ | |
using (ETradeContext context = new ETradeContext()) | |
{ | |
context.Products.Add(product); | |
context.SaveChanges(); | |
} | |
} | |
public void Update(Product product) | |
{ | |
using (ETradeContext context = new ETradeContext()) | |
{ | |
var entity = context.Entry(product); | |
entity.State = EntityState.Modified; | |
context.SaveChanges(); | |
} | |
} | |
public void Delete(Product product) | |
{ | |
using (ETradeContext context = new ETradeContext()) | |
{ | |
var entity = context.Entry(product); | |
entity.State = EntityState.Deleted; | |
context.SaveChanges(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment