Wednesday, December 5, 2012

Demystifying Entity Framework and Linq

In this blog, I want to share some knowledge of above subjects with you! Somewhere in the blog, there is a deliberate error in the facts! If you can spot it, do not keep your silence and report it, so that we can all benefit on the discussion!

What on earth is Entity Framework and how does it concern me?

African definition:
  • It has got something to do with using data in your application
  • How you interact with data in a database
  • It bridges the gap between above 2 points

To be able to use database data in an application, a whole bunch of code need to be written in a programming language such as C Sharp or VB.Net.

Entity Framework does all this for you!

High-tech (MSDN) Definition:
  • The Entity Framework is Microsoft’s recommended data access technology for new applications.
  • The Microsoft® ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write.
  • Using the Entity Framework, developers issue queries using LINQ, then retrieve and manipulate data as strongly typed objects.
  • The Entity Framework’s ORM implementation provides services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals.

Definition of Lazy Loading: Lazy loading is a design pattern commonly used in computer programming to initialize execution before the point at which it is needed.

This seems to be a mouthful, so how do I use Entity Framework to interact with the database?

How does LINQ make my life easier?

African definition:

  • Obviously, Linq get used in above subjects!
  • I want the Name, Surname and Contact number (SELECT) of a Customer (FROM) that lives in Gauteng (WHERE)...
  • or, turn it around: The Customers (FROM) that lives in Gauteng (WHERE), I want their Name, Surname and Contact numbers (SELECT)...

So, you use Linq to talk to the Database (or many other objects) and telling it what you want!

High-tech (MSDN) Definition:
  • Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic.
  • LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.
  • Language-Integrated Query (LINQ) is an innovation introduced in Visual Studio 2008 and .NET Framework version 3.5 that bridges the gap between the world of objects and the world of data.
  • Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support.
  • LINQ makes a query a first-class language construct in C# and Visual Basic. You write queries against strongly typed collections of objects by using language keywords and familiar operators.

Example of traditional SQL:

SELECT FirstName, LastName, City, ContactNo
FROM dbo.Persons
WHERE City = 'Pretoria'

Example of LINQ Query:

var query = from p on context.Persons
      where = “Pretoria”
      select p;

So, although they are working tightly together, Entity Framework and Linq are not depending on each other!

In the next blog, we will start a Visual Studio 2010 project, setup the Data Layer with Entity Framework, do some interesting stuff and then add a new project to do some queries against the Data Layer with some Linq queries!

Watch this space in 2 weeks, yep, just before Christmas!

So, did you get the error...?

No comments:

Post a Comment