Loading Related Data in Entity Framework

There are three behaviors that entity framework can use when loading related data. These are lazy loading, eager loading, and explicit loading. The main difference between the three are in when the related data gets loaded and whether it is done behind-the-scenes or explicitly. In this post we will learn about these behaviors and evaluate when one should be used over the other.

Using AntiForgery Tokens in ASP.NET MVC

Cross-site Request Forgery (CSRF or XSRF) is a type of cyber attack wherein an attacker makes an HTTP request on a user's behalf without the user's knowledge or consent. If successful, a significant amount of damage can be done, depending on the nature of the request. This kind of attack can be prevented by using antiforgery tokens. In this post, we will talk about how we can prevent CSRF attacks in our ASP.NET MVC applications.

Creating Custom Html Helper Methods Part 2

In a previous post we talked about creating custom html helpers. The implementation there involved customizing helpers that already existed in the ASP.NET MVC framework. In this post we will talk about creating our own helpers from scratch. This will allow us to create helpers for any kind of html element and will also provide flexibility on the element building process.

Creating Custom Html Helper Methods

HTML helper methods are used in Razor views to generate HTML markup in a strongly-typed, C-sharpy way. An example is @Html.LabelFor(m => m.Name), which is used in a strongly-typed view whose model has a Name property. This will generate a label element with the appropriate display name. In this post we will learn how to create a custom HTML helper.

Create Meaningful Classes

In a previous post I talked about the importance of good naming when writing code. The title of that post is "Don't Make Me Think... About Your Code". This post will talk about meaningful classes and can be considered as an addition to the "writing code" series.

Refactoring to the Single Responsibility Principle

In this post we are going to show a simple example of refactoring code to better adhere to the Single Responsibility Principle (SRP).

AngularJS and SEO

There is no doubt that JavaScript MVC / MVVM frameworks such as AngularJS provide better experiences for both developers and users alike. However, there are some caveats that we should be aware of when using this framework. Specifically, search engines might not be able to read the content in our applications if we are using the data binding features provided by these frameworks. In this post I will compare two scenarios: one using server-side data binding and one using client-side data binding and talk about their SEO implications.

Remove Custom Headers From Your ASP.NET MVC Project

Whenever we start with a new ASP.NET MVC project, the tendency is to use one of the templates offered by Visual Studio. The template goes a long way in getting us started with our project. However one thing that the template does not do is remove the HTTP headers that are related to ASP.NET and MVC. Today we are going to look at those and how and why to remove them.

Using AutoMapper in Entity Framework Projections

AutoMapper is a wonderful tool that lets us convert one class to another using fluently-defined configurations. It is useful when there is a need for multiple mappings, such mappings from persistence model classes to domain model classes or mapping from domain model classes to viewmodels. In this post we are going to look at how to use AutoMapper with Entity Framework for projections.

Converting an ASP.NET View to a String

Sometimes you would like to get the contents of a view and store it in a string rather than render it as HTML. An example of this is if you want to compose HTML emails and you want to harness the power of the ASP.NET MVC infrastructure (which includes layouts, partial views, and models). In this post I will show you how to store rendered View content in a string.

Separating Business Rules from Data Access

Business rules can be mixed into data access code. This scenario is a violation of the Single Responsibility Principle, because the data access class would have to change if the business rules change. In this post I will show you how to separate the business rules from the data access code.

Using NLog to Log to a Database with Entity Framework Code First

NLog is a logging framework that is used to add logging capabilities to a .NET application. Today I am going to show how to use NLog to generate logs on a database created by Entity Framework Code First.

Adding an SQL Azure Linked Server

When adding an SQL Azure Linked Server through SSMS, you will encounter an error if you go through the normal way (Server Objects > Linked Servers > New Linked Server). That is because the New Linked Server wizard does not allow you to specify the catalog name if the chosen Server Type is SQL Server, which should be chosen when adding an SQL Azure Linked Server. To accomplish this, the stored procedure sp_addlinkedserver should be used.