?
Solved

How to use Linq to entities

Posted on 2009-04-21
14
Medium Priority
?
671 Views
Last Modified: 2013-12-17
Hi!

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?
0
Comment
Question by:Nargzul
  • 6
  • 5
  • 3
14 Comments
 
LVL 21

Accepted Solution

by:
naspinski earned 1600 total points
ID: 24194006
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:
http://naspinski.net/post/Getting-started-with-Linq-To-Entities.aspx
0
 
LVL 21

Expert Comment

by:naspinski
ID: 24194036
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.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 24194101
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.

Fernando
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 1

Author Comment

by:Nargzul
ID: 24194178
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.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 24194250
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.
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 total points
ID: 24194272
0
 
LVL 1

Author Comment

by:Nargzul
ID: 24222385
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?
0
 
LVL 21

Expert Comment

by:naspinski
ID: 24222409
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?
0
 
LVL 1

Author Comment

by:Nargzul
ID: 24222583
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"?
0
 
LVL 21

Assisted Solution

by:naspinski
naspinski earned 1600 total points
ID: 24222657
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 
  where 
    p.username.Equals("Bryan") && 
    p.password.Equals("lkajdlskjadskfjaldsjf") &&
    isActive
  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

0
 
LVL 1

Author Comment

by:Nargzul
ID: 24222862
And, how can we know if the object loaded is correct? The var is null?
0
 
LVL 21

Expert Comment

by:naspinski
ID: 24223194
if the var is null, there is nothing in your db that matches that criteria exactly - could be case, spelling or anything like that.
0
 
LVL 1

Author Comment

by:Nargzul
ID: 24223267
I've an exception when there is no corresponding tupple, is it right?
0
 
LVL 21

Expert Comment

by:naspinski
ID: 24223303
tupple?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question