Friday, May 29, 2015

20 important ADO.NET Entity framework interview questions


ADO.NET Entity framework is one of the hottest topic now a days in .NET interviews , below are some Entity framework interview questions which you would like to revise before going to C# interviews.
  1. What is Entity framework?
  2. What’s the difference between LINQ to SQL and Entity framework?
  3. What are CSDL, SSDL and MSL?
  4. What is the work of EDMX file?
  5. How can we browse using entity framework classes?
  6. How can we add, update and delete using EF?
  7. How can we use stored procedures in entity frame work?
  8. What are POCO classes in Entity framework?
  9. How to implement POCO in entity framework?
  10. In POCO classes do we will need EDMX files?
  11. What is code first approach in entity framework?
  12. How can implement code first in entity framework?
  13. What is the difference between POCO, code first and simple EF approach?
  14. How can we handle concurrency in Entity framework?
  15. How can we do pessimistic locking in Entity framework?
  16. What is client wins and store wins mode in entity framework concurrency?
  17. What is Lazy Loading in Entity framework?
  18. How can we turn off lazy loading?
  19. What are navigation properties in Entity framework?
  20. What are complex types in Entity framework?

Entity Framework Interview Questions and Answers

ADO.NET Entity Framework is an advance framework developed by Microsoft Corporation. This article describes some common important interview questions with answers in ADO.NET Entity Framework.Hope it will help you to build successful carrier.
What is ADO.NET Entity Framework or EF?
ADO.NET Entity Framework is an ORM Framework developed by Microsoft. It is an enhancement of ADO.NET that gives us an automated mechanism to access and store data in the database. We can access database without much more code or programming.
ORM is a tool to store data from domain object to relational database like MS SQL Server without too much coding. It has three parts: Domain Class Object, Relational Database Object and Mapping information on how domain object maps with relational database objects
History of ADO.NET Entity Framework
  • The first version of Entity Framework released on 11 August, 2008 with .NET Framework 3.5 Service Pack 1 and Visual Studio 2008 Service Pack 1
  • The second version of Entity Framework was released on 12 April 2010 with .NET Framework 4.0
  • A third version of Entity Framework was released on April 12, 2011. Its name was Entity Framework 4.1
  • A refresh of version of Entity Framework 4.1 was released on July 25, 2011
  • The version Entity Framework 4.3.1 was released on February 29, 2012
  • The latest version is 5.0.0 and is targeted at .NET framework 4.5. But it is also available for .Net framework 4
  • The Entity Framework 6.0 is an open source project. Its source code is hosted at CodePlex using Git and licensed under Apache License v2. Like ASP.NET MVC Framework
What is minimum requirement for Entity Framework applications to run?
The Entity Framework is a component of the .NET Framework. Any application developed by Entity Framework, can run on any computer which has .NET Framework 3.5 SP or greater version.
What are the benefits of using Entity Framework or EF?
The main and the only benefit of EF is auto-generates code for the Model (middle layer), Data Access Layer, and mapping code. It reduces a lot of development time.
What is Entity Data Model (EDM)?
Entity Data Model or EDM refers to a set of concepts that describe data structure, regardless of its stored form. It is a bridge between application and data storage and helps us to work at a conceptual level rather than the actual schema. It uses three key concepts to describe data structure (entity type, association type and property).
What is .edmx file and what it contains?
An .edmx file (Entity Data Model XML) is an XML file. It defines a conceptual model, a storage model, and the mapping between these models. This file also contains the information that is used by the ADO.NET Entity Data Model Designer to render a model graphically. It contains all the mapping details of how objects map with SQL tables. It is divided into three sections: CSDL, SSDL, and MSL.
What are the main parts in EDM?
In entity framework, the EDM contains three main components.
  • Conceptual Model
    The conceptual model contains the model classes and their relationships. This will be independent from the database table design.
  • Storage Model
    The storage model is the database design model which includes tables, views, stored procedures, relationships and keys.
  • Mapping
    The mapping contains the information about how the conceptual model is mapped to storage model.
What are CSDL, SSDL and MSL sections in an EDMX file?
CSDL, SSDL and MSL are actually XML files.
CSDL (Conceptual Schema Definition Language) -is the conceptual abstraction which is exposed to the application.
SSDL (Storage Schema Definition Language) -defines the mapping with our RDBMS data structure.
MSL (Mapping Schema Language) -connects the CSDL and SSDL.
What are the languages used in entity framework?
In entity framework basically two languages are used.
  • LINQ to Entities
  • Entity SQL
  • Native SQL
What is Entity SQL?
Entity SQL is a SQL-like language that is used to query in the conceptual models in the Entity Framework. The conceptual models represent data as entities and relationships. Entity SQL allows querying those entities and relationships in a format that is familiar to those who have used SQL. But it is more difficult than LINQ to Entities.
What is LINQ to Entities?
LINQ to Entities is a query language used to write queries against the object model. It returns entities, which are defined in the conceptual model. LINQ to Entities provides developers to write LINQ queries.
What is Native SQL?
In Native SQL, we can write native SQL queries for relational database. A sample example is given bellow:
using (var ctx = new TestDBEntities())
{
    var studentName = ctx.Students.SqlQuery("Select StudentID, StudentName from Students where StudentName='AAA'").FirstOrDefault<Student>();
} 
What are the types of entities in entity framework?
In Entity Framework 5.0/6.0 there are two types of entities:
  • POCO entity (Plain Old CLR Object)
    It is like our normal .Net classes.
  • Dynamic proxy entity
What is the difference between DbContext and ObjectContext?
DbContext is a lightweight version of the ObjectContext class. DbContext requires a lot less code for the same kind of functionality. ObjectContext EF V4.0 and DbContext EF V4.1
What are the main drawbacks of Entity Framework?
The main drawbacks of EF are lazy loading. It is EF default setting but we can disable it. Due to this behavior if we are loading large number of records (especially if they have foreign key relations) then it may take long time. So for better performance disable lazy loading for large number of records.
How to load related Entities in EF
In Entity Framework we can load related data in three ways
  1. Eager loading
  2. Lazy loading
  3. Explicit loading
What is Lazy Loading?
Lazy loading is the process to delay the loading of related objects until we need it. Other hand, on demand objects loading rather than loading objects unnecessarily. When objects are returned by a query related objects are not loaded at the same time.
[ Fist query will load main object and the related objects will be loaded in second queries.]
What is Eager Loading?
The opposite of Lazy Loading is eager loading. Eager loading is the process of loading related entities/ objects.
What is Explicit Loading?
If lazy loading disabled it is still possible to lazily load related entities. For this we need to call the Load method on the related entities.
How can we turn off lazy loading?
We can turn off lazy loading by setting LazyLoadingEnabled to false.
context.ContextOptions.LazyLoadingEnabled = false;
When we will use lazy Loading?
When we know that only main object will be used and related objects will not be used.
What are the types of development approaches in EF?
The Entity Framework supports three different development approaches to use entity framework in our applications.
  • Code First
  • Model First
  • Database First
What is Code First approach in Entity Framework?
In Code First approach we avoid working with the Visual Designer of Entity Framework. We can say, the EDMX file is excluded from the solution. So now, we have complete control over the context class as well as the entity classes. Here the developers writes POCO classes first and then generates the database from these POCO classes. The developers, who follow Domain-Driven Design (DDD) principles, prefer to use code first model.
What is Model First Approach in Entity Framework?
In Model First approach, we can create Entities, relationships directly on the design surface of EDMX. So in this approach, when we want to add ADO.NET Entity Data Model in our application, we should select “Empty Model” instead of “Generate from database” in the entity data model wizard. By right clicking on the designer surface we can create entity, association, inheritance, database etc.
What is Model First Approach in Entity Framework?
In model first approach, we create EDMX from an existing database. We can update EDM any time based on database schema changes. Its support stored procedure, view, etc.
Which development approach is suitable for which conditions?
  • Code First
    If we have existing application with domain classes
    Want to create database from your existing domain classes
  • Model First
    If we have existing database
  • Database First
    If we don’t have existing database or domain classes, and we prefer to design our db model on the visual designer
Entity Framework vs LINQ-to-SQL
Entity Framework and LINQ to SQL are not same. There is some difference. The difference between Entity Framework and LINQ to SQL:
  • EF has a full provider model. It supports SQL Server, Oracle, DB2, MySQL etc
  • Most of the time L2S classes must be one-to-one with database objects (Customer class can be mapped only with Customer table). Where in EF we can map our domain class with multiple tables using various inheritance strategies like table per type (class) or table per hierarchy of classes etc
  • EF supports multiple modeling techniques (code first, model first or database first)
  • Microsoft Corporation has long term strategy to support and integrate Entity Framework with multiple Microsoft products
What do you mean by Navigation Property?
A navigation property is an optional property on an entity/object that allows for navigation from one end of an association to the other end. Unlike other properties, it does not carry data.
What are POCO classes in Entity Framework?
POCO means Plain Old C# Object.
In POCO classes do we need EDMX files?
Yes we need. Because the context objects reads the EDMX files to do the mapping.
What are T4 templates?
Text Template Transformation Toolkit or T4 is a template based code generation engine. We can go and write C# code in T4 templates (.tt is the extension) files and those C# codes execute to generate the file as per the written C# logic.
T4 C# code:
<#@ template language=”“C#”” #>
Hello <# Write (”Mr.!”) #>
C# output:
Hello
Mr. !
What is the importance of T4 in Entity Framework?
T4 files are the heart of Entity Framework code generation. It read the EDMX XML file and generates C# behind code. This C# behind code is our entity and context classes. If we developed our project in VS 2012 then we can found .tt files.
Give an example of EF select query
An example of EF query that returns CustomerID, CustomerName, Address from Customers table with gender male is given bellow:
TestDBEntities db = new TestDBEntities();
var result = from c in db.Customers
             where c.Gender == 'M'
             orderby c.CustomerName ascending
             select new { c.CustomerID, c.CustomerName, c.Address };
Give an example of EF join query
An example of EF query that returns CustomerID, CustomerName, OrderDate, Order Amount from Customer and Orders table is given bellow:
TestDBEntities db = new TestDBEntities();
var result = from c in db.Customers
             join o in db.Orders on c.CustomerID equals o.CustomerID
             where c.CustomerID == 1
             select new { c.CustomerID, c.CustomerName, o.OrderDate, o.Amount };
Write the query to return a generic list in EF
An example of EF query that creates a generic list of Customer table is given bellow:
TestDBEntities db = new TestDBEntities();
var result = from c in db.Customers                      
             select new { c.CustomerID, c.CustomerName };

IList<object> items = new List<object>();
foreach (var r in result)
{
    items.Add(r);
}
Give an example of EF insert query
An example of EF query that insert data into Students table is given bellow:
TestDBEntities db = new TestDBEntities();
Student oStudent = new Student();
oStudent.StudentName    = "Mr. KK Modi";
oStudent.Address        = "Mumbai";
oStudent.Phone          = "9912548";
db.Students.AddObject(oStudent);
db.SaveChanges();
Give an example of EF update query
A sample EF query to update Students tables is given bellow:
TestDBEntities db = new TestDBEntities();
var result = (from S in db.Students
              where S.StudentID == 2
              select S).Single();

result.StudentName  = "Mr. John Abrar";
result.Address      = "6/2 Park Road";
result.Phone        = "9658745";
db.Students.ApplyCurrentValues(result);
db.SaveChanges();
Give an example of EF delete query
A sample EF query to delete data from Students table is given bellow:
TestDBEntities db = new TestDBEntities();
var result = (from S in db.Students
              where S.StudentID == 3
              select S).Single();

db.Students.DeleteObject(result);
db.SaveChanges();
How to call stored procedure in Entity Framework?
In order to use stored procedure (SP) in EF we need to follow two steps.
  • Add the stored procedure in EDM
    We can add stored procedure in EDM as like table insertion process. But if need to add SP in existing EDM, then right click on designer and click on “Update model from database…”. A popup update wizard will be open where we can select stored procedures and add it. Now we have to map this stored procedure to the conceptual model. For that we have to follow second step.
  • Add function import
    Right click on the designer surface and select “Model Browser”. From the Stored Procedures node, right click on the stored procedure and select “Add Function Import…”. Select four types of return values and click ok. Now we can write codes in to access our SP.
  • entity framework model browser
Consider we have two stored procedure TestProcedure1, and TestProcedure2. The procedure TestProcedure1 has no parameter, but TestProcedure2 has two integer type parameters. Sample EF query that call a stored procedure (SP) is given bellow:
TestDBEntities db = new TestDBEntities();
GridView1.DataSource = db.TestProcedure1();
GridView1.DataBind();

GridView2.DataSource = db.TestProcedure2(1, 5);
GridView2.DataBind();
What are the return types of stored procedure in entity framework?
When we include a stored procedure in a EDM then, we can select four types of return values. A small descriptions is given bellow.
  • None
    None means stored procedure will not return any value.
  • Scalars
    Scalars means stored procedure will return single value of selected type like Boolean, binary etc.
  • Complex
    Complex means stored procedure will return complex type which is only on conceptual model but not in database table.
  • Entities
    Entities means stored procedure will return collection of selected entities as like table.

ADO-NET-Entity-Framework-Interview-Questions

View State Vs. Session State Vs. Application State

scrum or agile

http://www.mountaingoatsoftware.com/agile

What does a Scrum Master do all day?

What the Scrum Master doesn’t do…

We know the Scrum Master is a crucial player on new Scrum teams, but what exactly does he or she do all day?
For starters, we know the Scrum Master doesn’t plan the release, because that’s done by the product owner and the team. We know he doesn’t manage the developers because the Scrum team is self-organizing; and we know he’s not even the guy who’s accountable if the end result sucks (that’s the product owner too).

So what’s left?

If a product owner is like the head that makes decisions, and a Scrum team is like the body that executes your plan, then the Scrum Master could be considered the ooey-gooey insides that hold everything together.
More simply put, the Scrum Master takes on the administrative, coaching and leadership roles that make Scrum development possible. That means he’ll usually spend his days:
  • Facilitating (not participating in) the daily standup
  • Helping the team maintain their burndown chart
  • Setting up retrospectives, sprint reviews or sprint planning sessions
  • Shielding the team from interruptions during the sprint
  • Removing obstacles that affect the team
  • Walking the product owner through more technical user stories
  • Encouraging collaboration between the Scrum team and product owner
Notice the common theme… Almost everything that ties into Scrum will be directly facilitated by the Scrum Master or create a situation where the Scrum Master can provide guidance. The point here is to show that the Scrum team should not overly concern themselves with Scrum and stay focused on the job of software development. Similarly, the product owner can continue honing in on business needs with the confidence that the Scrum Master will pull her in at key points like sprint reviews. The Scrum Master champions these duties to ensure that Scrum processdoesn’t impede team progress.

Do we need a dedicated Scrum Master?

Although these tasks are often enough to keep someone busy all day, not every Scrum Master is “just” the Scrum Master. Some teams may choose to elect a developer or tester to become Scrum Master for the team, because they don’t feel the position merits full-time personnel. Based on the duties a Scrum Master performs, your team may be able to get away without a dedicated individual if:
  • Your product owner knows everything about the customer and is always there for the development team without guidance from the Scrum Master.
  • Your dev team has such a healthy communication culture that daily standups are redundant and add to the overall process overhead.
  • The burndown chart and other artifacts are maintained automatically and don’t create any overhead for the development team.
  • The team operates free of distractions and can easily clear all obstructions on their own.
It’s reasonable for such a mature team to select a Scrum Master from within, but these high levels of cohesion aren’t likely from organizations that are just starting out. Whether or not your team has exceptional expertise, the Scrum Master role is crucial because it acts as a buffer between the team and any process overhead Scrum can create.

Takeaways

Now that we’ve figured out what a Scrum Master does all day, here are some key points:
Scrum Masters…
  • Take on the administrative, coaching and leadership roles that make Scrum development possible.
  • Ensure the Scrum process doesn’t impede team progress.
  • Act as a buffer between the team and process overhead, so each team member can focus on shipping the software on time.

Wednesday, May 27, 2015

MVC3 Vs MVC4 Vs MVC5 Vs MVC6

MVC3 Vs MVC4 Vs MVC5 Vs MVC6
Note: If you wanted to get a practical example for building your first ASP.NET MVC 5 Application using Entity Framework, please follow here.
MVC3 Vs MVC4 Vs MVC5

ASP.NET MVC3

  • New Project Templates having support for HTML 5 and CSS 3.
  • Improved Model validation.
  • Razor View Engine introduced with a bundle of new features.
  • Having support for Multiple View Engines i.e. Web Forms view engine, Razor or open source. You can follow here for a detailed comparison on difference between WebForm View Engine and Razor View Engine.
  • Controller improvements like ViewBag dynamic property and ActionResults Types etc. Dynamic property is a new feature introduced in C# 4.0. ViewBag being a dynamic property has an advantage over ViewData that it doesn’t require checking NULL values. For detailed difference between ViewBag and ViewData can be found here.
  • Unobtrusive JavaScript approach that actually separates the functionality from presentation layer on a web page.
  • Improved Dependency Injection with new IDependencyResolver.
  • Partial page output caching.

ASP.NET MVC 4

  • ASP.NET Web API, a framework that simplifies the creation of HTTP services and serving a wide range of clients. Follow to create your first ASP.NET Web API service.
  • Adaptive rendering and other look-n-feel improvements to Default Project Templates.
  • A truly Empty Project Template.
  • Based on jQuery Mobile, new Mobile Project Template introduced.
  • Support for adding controller to other project folders also.
  • Task Support for Asynchronous Controllers.
  • Controlling Bundling and Minification through web.config.
  • Support for OAuth and OpenID logins using DotNetOpenAuth library.
  • Support for Windows Azure SDK 1.6 and new releases.
Exam 70-486

ASP.NET MVC5

  • ASP.NET Identity for authentication and identity management. Thesedays, modern applications are developed for broader range of clients such as web, mobile in mind. Also, users are actively using their social identities from various social channels like facebook, youtube, twitter etc. ASP.NET Identity is a new Membership system to handle authentication and authorization for variety of clients as well as using user’s existing social identities.
  • Authentication Filters for authenticating user by custom or third-party authentication provider.
  • With the help of Filter overrides, we can now override filters on a method or controller.
  • Bootstrap replaced the default MVC template.
  • Attribute Routing is now integrated into MVC5. Basically, MVC Routing is an excellent way to create human friendly and Search Engine Optimized URLs. You can easily get understanding about Routing in ASP.NET MVC here. Attribute based routing enables us to define routes along with action methods as follows:
  [Route(“Students/{id}”)]
  public ActionResult GetStudentById(string id)
{        
           // code logic here.        
           return View();
}

ASP.NET MVC6 | ASP.NET vNext

  • Single Programming Model for ASP.NET MVC and ASP.NET Web API.
  • Optimized for Cloud Computing.
  • Supporting side by side deployment of runtime and framework along with application.
  • Out of the box support for dependency injection.
  • vNext is Open Source and supports running on multiple platforms including Linux and Mac.
  • New JSON-based project Extension.
  • In order to dynamically compile code, Roslyn compiler is used.
Hopefully, this article will help you in comparing core features of ASP.NET MVC in different versions.

ViewData vs ViewBag vs TempData vs Session

ViewData vs ViewBag vs TempData vs Session

Posted By : Shailendra Chauhan, 19 Jul 2012
Updated On : 05 Apr 2015
 Total Views : 179,938   
 Version Support : MVC5, MVC4 & MVC3
 
Keywords : when to use viewbag, viewdata and tempdata, differences between viewdata, viewbag, session and tempdata in mvc3 mvc4, tempdata keep method
In ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view and in next request. Like WebForm, you can also use Session to persist data during a user session. Now question is that when to use ViewData, VieBag, TempData and Session. Each of them has its own importance. In this article, I am trying to explain the differences among these four.

ViewData

  1. ViewData is a dictionary object that is derived from ViewDataDictionary class.
    1. public ViewDataDictionary ViewData { get; set; }
  2. ViewData is a property of ControllerBase class.
  3. ViewData is used to pass data from controller to corresponding view.
  4. It’s life lies only during the current request.
  5. If redirection occurs then it’s value becomes null.
  6. It’s required typecasting for getting data and check for null values to avoid error.

ViewBag

  1. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  2. Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
    1. public Object ViewBag { get; }
  3. ViewBag is a property of ControllerBase class.
  4. It’s life also lies only during the current request.
  5. If redirection occurs then it’s value becomes null.
  6. It doesn’t required typecasting for getting data.

TempData

  1. TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
    1. public TempDataDictionary TempData { get; set; }
  2. TempData is a property of ControllerBase class.
  3. TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).
  4. It’s life is very short and lies only till the target view is fully loaded.
  5. It’s required typecasting for getting data and check for null values to avoid error.
  6. It is used to store only one time messages like error messages, validation messages. To persist data with TempData refer this article: Persisting Data with TempData

Session

  1. In ASP.NET MVC, Session is a property of Controller class whose type is HttpSessionStateBase.
    1. public HttpSessionStateBase Session { get; }
  2. Session is also used to pass data within the ASP.NET MVC application and Unlike TempData, it persists for its expiration time (by default session expiration time is 20 minutes but it can be increased).
  3. Session is valid for all requests, not for a single redirect.
  4. It’s also required typecasting for getting data and check for null values to avoid error.
Summary
In this article I try to explain the difference between ViewData, ViewBag and TempData. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.