Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Wednesday, 18 February 2009

ADO.NET Entity Framework Model

ADO.net entity framework model represents an object model that contains classes (LINQ To SQL ) entities, and association between objects.These objects are based & mapped to the datasource we provide.

Object relational designer is used to create entities.Lets create a Northwind database Object Model and query it through LINQ.I am using ADO.net Entity framework wizard for quick start.
On solution right click, Add new item.
Go to Template and select ADO.net Entity Framework Model.

Name it with Nortwindsamplemodel.In th wizard Generate from database.

in next step , choose connection to datasource and datasource.
  • click on new Connection
select server name from dropdown list.
choose authentication options.
select database from dropdownlist.(In example i have used Northwindump database which is a replicated database of northwind)
Test connection and press OK
for th System administrator password (sa) i am keeping password in .config file for simpilicty of this tutorial therefore i have select the option in the step given
->Yes include the sensitive data ine connection string.
Now select the database objects you want to include as entities.

Designer creates .edmx file with database entities(tables,views,stored procedure) as object on designer surface and also creates association based on Primary key,Foreign key relationship between tables in database.Every object is mapped exactly to one matching table in database.There is no option to map one object to joined table in ADO.net entity framework.

Below figure shows Created Entities(Objects based on tables) and associations.

Now our model is complete and we just need to query the data via the model.

We will use LINQ for this purpose.(Note that DataContext is the main class to send and receive the data from database however we dont code it by our self as wizard has done it)
Lets create store and query category object through LINQ.

using (NorthWindDumpEntities NorthwindStore=new NorthWindDumpEntities())
{
var _categorylist = from _catlist in NorthwindStore.Categories
select _catlist;
foreach (Categories c in _categorylist)
{
listBox1.Items.Add(c.CategoryName);
}

}

Conclusion. ADO.net entity together with LINQ saves greater time to do same thing by creating objects manually and doing a lot of coding.

Wednesday, 11 February 2009

LINQ Basics

Language Integrated Query is .net 3.0 new features for querying data source where data source is in the form collection of objects. It can be simple array, classes or sql databases.You can say simple "It is general type query language in .net World that can be applied to a variety of data sources."
Advantage of LINQ over conventional query languages(like SQL for databases) has one pattern of coding querying data source. For example Oracle or SQl server, posgres has different syntax of SQL and if one has to query that data sources in their relative syntax, one has to learn the syntax of each data source sql syntax.Other Advantage is of course is highly scalable object oriented code in n- tier Architecture.


LINQ query is executed through foreach statement and foreach statement requires that object that is quried should implement IEnumerable Interface.That is mandatory.so if you query a simple array , it can be queried easily with LINQ as it by default implments IEnumerable interface.All types that implments this interface are called queryable types.


Let start with simple string of array as datasource and execute it through LINQ.I will use Visual studio 2008 for example and i see

using System.Linq; namespace by default in windows forms application.


Open Windows form application. drag & drop Listbox & a button control on form. write this code on button click event.


private void button1_Click(object sender, EventArgs e)

{

String[] MoviesList = new String[] {"Dantes peak","Twister","Shashank redemption","Trueman show","National Treasure" };

var moviesn = from greatmovies in MoviesList

select greatmovies;

foreach (String favMovies in moviesn)

{

listBox1.Items.Add(favMovies);

}

}









Result is shown in screen shot below.




















I will discuss LINQ in depth with LINQ to SQL features later.