How do we use linq in mvc? And wha is entity framework? [Resolved]

Posted by Kumarkrishna184 under ASP.NET MVC on 12/11/2015 | Points: 10 | Views : 682 | Status : [Member] | Replies : 7
How do we use linq in mvc? And what is entity fraamework?

Thanks and Regards,
Krishna Kumar



Responses

Posted by: Rajnilari2015 on: 12/12/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
How do we use linq in mvc?

LINQ can be use in MVC only in the Controller. However, we can use LAMBDA expression both in VIEW and Controller. Below is an example for your reference.

Suppose we have a model as under

public class CreateEmpModel


{

[Display(Name = "First Name")]
[Required(ErrorMessage = "First name required")]
public string FirstName { get; set; }


[Display(Name = "Last Name")]
[Required(ErrorMessage = "Last name required")]
public string LastName { get; set; }
}


and the Controller is written as under

public class EmpController : Controller


{

private readonly IEmpService empService;

public EmpController(IEmpService empService) //this is for dependency injection
{
this.empService = empdeptService;
}

public ActionResult Create()
{
CreateEmpModel viewModel = new CreateEmpModel();
return View(viewModel);
}
}


The view can be written as under using LAMBDA

@model ViewModels.CreateEmpModel


<table>

<tr>

<td><b>First Name:</b></td>

<td>@Html.TextBoxFor(x => x.FirstName, new { maxlength = "20", size = "20" }) //Note the use of Lambda Expression here

@Html.ValidationMessageFor(x => x.FirstName)

</td>
</tr>

<tr>

<td><b>Last Name:</b></td>

<td>@Html.TextBoxFor(x => x.LastName, new { maxlength = "20", size = "20" }) //Note the use of Lambda Expression here

@Html.ValidationMessageFor(x => x.LastName)

</td>

</tr>

</table>


As can be figure out that, we are quering the model in the VIEW by using LAMBDA Expression.


If we want to use LAMBDA expression in Controller, the below is an example

public class EmpController : Controller


{

private readonly IEmpDeptService empdeptService;

public EmpController(IEmpDeptService empdeptService) //this is for dependency injection
{
this.empdeptService = empdeptService;
}

public ActionResult FindEmployeeByID(in empID)
{
EmpModel empRecords = GetEmployeeRecords().Where(i=>i.EmpId == empID); //by using LAMBDA we are filtering the employees

return View(empRecords);
}
}


An equivalent LINQ will be

public ActionResult FindEmployeeByID(in empID)

{
EmpModel empRecords = (from emp in GetEmployeeRecords()
where emp.EmpId == empID
select emp).ToList();
return View(empRecords);

}


You can refer to http://techfunda.com/Howto/asp-net-mvc/262/list-data-from-more-than-one-tables-models for a step by step approach about the same.

Also my article on Building a CRUD using MVC 4 using Repository pattern as the model - Step by step ( http://www.dotnetfunda.com/articles/show/2339/building-a-crud-using-mvc-4-using-repository-pattern-as-the-model-step ) will help you to apply the techniques.

Hope this helps

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/12/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
What is entity framework?

Entity Framework (EF) is an object-relational mapper (ORM) that enables us to work with relational data using domain-specific objects. The earlier mechanism of dealing this was ADO.net. Later on MS introduce their first attempt to make an ORM by using LINQ to SQL. Though not asked in the question, however, we feel it is important at this juncture to mention that ORM is a programming technique that contains a set of classes that map relational database entities to objects in a specific programming language.

Some of the advantages of EF or any ORM tool like (EF, NHibernate,Hibernate,GORM etc.) are as under -

- System will create objects as well as track changes on those objects and simplify the process of updating/querying the database thereby eliminating a large chunk of code to be written and maintain by us using ADO.net way.

- Since the mapping between the domain objects and database objects is specified declaratively (SSDL, CSDL and C-S mapping present inside the .Edmx file) and not in code, if we need to alter the database schema, we can minimize the impact on the code.In the EDMX designer we onlt need to refresh and the altered model will be loaded without impacting the earlier OR design.

- We can easily ADD/Delete and object (tables, SP,Views) from the Edmx designer.

- Deferred execution

- Lazy loading and eager loading

- EF can be use with many of DB's (e.g. SQL Server, RavenDB,Oracle, MySQL, Postgres,SQL CE etc.)

- Parent Child relationship mapping can be done very elegantly and eloquently using EF Designer.

- We can create inheritance (e.g. Table-per-Type (TPT) ,table-per-hierarchy (TPH)) in EF.

- Easy stored procedure mapping in terms of functions

- Code will be safer from SQL Injection, Cross-Site Scripting (XSS)

- Code first Migration

- Querying Asynchronously there by speeding up the overall application process

- Database command logging

- Better Transaction Support

- Pluggable pluralisation and singularization service (you can refer to my article http://www.dotnetfunda.com/articles/show/2260/let-us-learn-pluralizationservices-of-net-40 about what they are )

- Better unit testing
etc.....

EF can be use in two ways.

a) If we are creating a new project with Domain Driven Design (DDD) , we must go ahead with Code First Approach .
b) If we are working on a legacy application , then database first approac h meets the requirement.

You can refer to my article http://www.dotnetfunda.com/articles/show/1742/task-management-systemtms as how to use EF in the project.

Hope this helps.




--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 12/12/2015 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. If you use EF, you do not need to use any data access related code like ADO.NET etc. Entity Framework does all job for you and give you an DbContext object that you can use to work with the database tables.

LINQ is Language INtegrated Query that helps us to query the collection of objects by writing server side code like C# or VB.NET. So we can use LINQ to query the Entity Framework DbContext domain-specific objects properties to query the data or even to insert, update and delete the data.

As Rajnilari2015 rightly explained, you can exactly follow that step. If you are looking for detailed knowledge of how to work with Entity Framework and LINQ in ASP.NET MVC, this is the right place to go http://techfunda.com/Howto/asp-net-mvc/entity-framework.

Hope this helps.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 12/12/2015 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Oops, sorry Rajnilari2015, as soon as I hit Post button on response the auto refreshed latest response shown your response on Entity Framework.

You have explained Entity Framework nicely. Thanks and keep it up!


Regards,
Sheo Narayan
http://www.dotnetfunda.com

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/12/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Sheo Sir, thanks for your motivational words.

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kumarkrishna184 on: 12/13/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks, Sheo Sir and Niladri Sir.......

Thanks and Regards,
Krishna Kumar

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/13/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Kumarkrishna184, you are welcome Sir and glad to know that all the answers presented here has helped you.

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response