Do you know why to use Entity Framework?
Loading last updated info...
Entity Framework allows you to provide a strongly typed object framework (ORM) over your database. This helps abstract the database away allowing it to be replaced with other technologies as needed.
using (SqlConnection conn = new SqlConnection()){conn.ConnectionString = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True";conn.Open();SqlCommand cmd = conn.CreateCommand();cmd.CommandText = "SELECT * FROM Customers WHERE CompanyName LIKE '" + companyNameTextbox.Text + "%'";bindingSource1.DataSource = cmd.ExecuteReader();}
❌ Figure: Figure: Bad example - using ADO.NET and not strongly typed
var results = dbContext.Customers.Where(c => c.CompanyName.StartsWith(companyNameTextbox.Text));customersBindingSource.DataSource = results;// Or evenvar results = dbContext.Customers.Where(c => c.CompanyName.StartsWith(companyNameTextbox.Text)).Select(c => new{c.CompanyName,c.Phone});customersBindingSource.DataSource = results;
✅ Figure: Figure: Good example - at least 4 good reasons below
- Making queries that are independent from specific Database engine
- Strongly typed fields - SQL tables/entities have intellisense
- More readable and less code
- It's easy to chain more operation like
OrderBy,GroupBy,FirstOrDefault,Count,Anyand many more - Developers are generally poor at writing SQL, so using code constructs makes writing queries much easier