How to use Linq to entities


I'm trying to use an LINQ to entities connection to connect my ASP application to the database(MS SQL), but, after many research, I don't find how to create model from the database, how to make it, Is there a new element to create in visual studio?
Who is Participating?
naspinskiConnect With a Mentor Commented:
Make your ADO.NET Entity Data Model (.edmx file)
This is comparable to the .dbml file that you made with L2S.

-Right click on your App_Code folder and select New Item
Select ADO.NET Entity Data Model and name it (I left the default Model.edmx for the example)

-Choose Generate from database and click Next

-Choose your database from the dropdown and choose the name to save the ConnectionString as and click Next

-Choose all the things you want included, I just chose tables, but you may want to include views and SPs if you have them

-*Be sure to remember the Model Namespace you choose for your .edmx (I will use DatabaseModel for the example) - click Finish

That is from my 'Getting started with Linq-To-Entities tutorial' you can read at:
by the way, I would recommend doing your research before commiting to L2E - IMO it is still immature compared to other ORMs such as Linq-to-SQL, NHibernate, etc.
Fernando SotoRetiredCommented:
Hi Nargzul;

To create the model for the Linq to Entity in your project do the following:

1.  Go to the Solution Explorer and right click on project name and select Add -> New Item
2.  In the Add New Item window select ADO.Net Entity Data Model and click Add button.
3.  When the Entity Data Model Wizard window opens select the Generate from Database option
4.  Click on the Next button.
5.  In the Choose Your Data Connection Select the connection to the Database or create a new one.
6.  Click on the Next button.
7.  In the Choose Your Database Objects select the Tables, Views and Stored Procedures to be in the model.
8.  Click on Finish.
9.  The Data Model will then be created and the design surface will be displayed.

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

NargzulAuthor Commented:
I've saw many times in many tutorial  this "Ado.Net ENtitiy data model", but when I go on the new item, I don't find anthing containing "ADO".

Must we install anything?

I've visual studio 2008.
Fernando SotoRetiredCommented:
Yes, Linq to Entity was added to VS 2008 with VS 2008 SP1. You will need to go to the Microsoft update site and update VS 2008 and .Net Framework 3.5 with SP1, Service Pack 1.
Fernando SotoConnect With a Mentor RetiredCommented:
NargzulAuthor Commented:
Thanks for the help. I've now a model. in your link, I don't see how to get object from database.

Creating is not problem, but how do we get an information?
a quick example is shown with a gridview:

Display your data
Now that we have put a few objects in to the database, we can go and check it out. Later we will dig in with code, but first we will use a LinqDatasource/GridView:
Drag a LinqDataSource (LDS) on to the page and click Configure Data Source from the little square on the upper right of the LDS
Choose your Object Model and click Next
Choose your table and click Finished
Drag a GridView (GV) on to your page
In the GV option box, choose your datasource from the dropdown

now just view your page:

Is that what you were looking for?
NargzulAuthor Commented:
But by exemple, to check that a user is valid, something like the equivalent of:

select * from users where username="Bryan" AND password="lkajdlskjadskfjaldsjf" And isActive="true"

geststockEntities gse = new geststockEntities();
 User usr = gse.User.First(u=>u.Username==this.username.Text);

But how can I have many condition in the "where clause"?
naspinskiConnect With a Mentor Commented:
there are a couple ways to do this with linq, the 'sql like way and with lambdas - in the below examples 'Entities' is the name of your entities context.
var users = from p in Entities.users 
    p.username.Equals("Bryan") && 
    p.password.Equals("lkajdlskjadskfjaldsjf") &&
  select p;
//this will actually get an IEnumerable of users - if you just want one do this:
var user = users.First();// you could ocmbine this into the above code as well
//Here is with Lambdas:
var users = Entites.Users.Where(p => p.username.Equals("Bryan") && p.password.Equals("lkajdlskjadskfjaldsjf") && isActive);
/or for a single record:
var user = Entites.Users.First(p => p.username.Equals("Bryan") && p.password.Equals("lkajdlskjadskfjaldsjf") && isActive);

Open in new window

NargzulAuthor Commented:
And, how can we know if the object loaded is correct? The var is null?
if the var is null, there is nothing in your db that matches that criteria exactly - could be case, spelling or anything like that.
NargzulAuthor Commented:
I've an exception when there is no corresponding tupple, is it right?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.