C# 6 Read-only auto properties

A while ago, when I was looking at read-only auto-properties I started comparing them to the previous method of making a property read-only by making it private. When it comes down to it there are a few differences how they can be initialised and read/written to. At the time I put together this example. Comments explain where and how each property can be accessed.

void Main()
{
	Person firstPerson=new Person(new DateTime(2001, 10, 16));
	
	// Unrestricted access to FirstName and LastName
	firstPerson.FirstName="John";
	firstPerson.LastName="Jones";
	
	// Cannot access Date Of Birth, other than via consructor or within class membes internally
	//firstPerson.DateOfBirth= new DateTime(1926, 03, 27);
	//firstPerson.Age = 34;
		
	// Accessing readonly property directly produces a compile time error
	firstPerson.ChangeDateOfBirth(new DateTime(1926, 03, 27));
	
	// Uncomment if runnning with LinqPad
	//Console.WriteLine(firstPerson);;
}

public class Person{

	// Unrestricted access
	public string FirstName{get; set;}	
	public string LastName{get; set;}	
	
	// Accessible only by Constructor
	public DateTime DateOfBirth {get;} // Date
	
	// Accessible by Contructors and internally to the class members
	public int Age {get; private set;}
	
	
	public Person(DateTime dob){
		DateOfBirth = dob;
		
		Age=-1;
		CalculateAge();
	}
			
	public void CalculateAge(){
		// Validat Date set before calculation
	
		// Implementation to calculate the age from the DateOfBirth
		DateTime today = DateTime.Today;
		Age = today.Year - DateOfBirth.Year;
	    if (today.Month < DateOfBirth.Month || (today.Month == DateOfBirth.Month && today.Day < DateOfBirth.Day))
		{
        	Age--;
		}
	}
	
	public void ChangeDateOfBirth(DateTime dateOfBirth){
		// Compile time error when trying to access readonly property
		//DateOfBirth = dateOfBirth;
	}
}

I have made this example available as a Gist here

Object Serialization and Deserialzation in c#

Previously I have written about how to serialize an object to XML. This post is an improved version of that post with better code and the addition of a deserialize example.
Required Namespace to be imported for serialization is System.Xml.Serialization.

This example is also available as a gist

public class Person
{
    public string Name {get; set;}
    public int Age {get;set;}
    public Address Address{get;set;}
}

public class Address
{
    public string Address1 {get;set;}
    public string Town {get;set;}
    public string PostCode{get;set;}
}

void Main()
{
    var p = new Person{
        Name = "John Jones",
        Age = 40,
        Address = new Address{
        Address1= "Daisy Meadow",
        Town="Chorville",
        PostCode = "CH1 1HC"
    }
};
	
XmlSerializer xmlSerializer = new XmlSerializer(p.GetType());
	
var xmlText = string.Empty;
using (TextWriter textWriter = new StringWriter()){
    xmlSerializer.Serialize(textWriter, p);
    xmlText = textWriter.ToString();
}

XmlReaderSettings settings = new XmlReaderSettings();
using(StringReader textReader = new StringReader(xmlText)) {
    using(XmlReader xmlReader = XmlReader.Create(textReader, settings)) {
         ((Person)xmlSerializer.Deserialize(xmlReader)).Dump();
        }
    }
}

Handling Complex Fake or Test Data When Unit Testing-Builder Pattern

The data supporting the unit tests you are creating may have classes that contain other classes as properties. Here we have a Person class that contains an Address class

public class Person{
	public string FirstName {get;set;} 
	public string LastName {get;set;}
	public Address Address{get;set;}
}

public class Address{
	public string Address1{get;set;}
	public string PostCode{get;set;}
	public string Country{get;set;}
}

There are a number of possible scenarios to be unit tested, i.e. If the address is missing or invalid or missing person details. For each unit test the test objects will need to be created which could easily result in duplicate or just tedious typing. For example objects similar to the one below may need to be created with slightly different data for each test.

var person = new Person{
	FirstName="Joe",
	LastName="Bloggs",
	Address=new Address{
		Address1="1 The Grove",
		PostCode="PembroVille",
		Country="United Kingdom"	
	}
};

A real life scenario could involve more complex classes than this example resulting in more tedious duplication. One useful trick is to create a builder class to easily build objects that cater for different test scenarios. In this builder class you can configure the data for each different positive and negative test with the advantages of configure once, reusable code.
Below is a PersonBuilder class which aids in building a Person object with different data relevant to the tests being performed.

public class PersonBuilder
{
    private Person _person = new Person();

    public Person Build(){
        // Set standard default values for properties here if needed
        return _person;
    }

    public PersonBuilder WithValidDetails()
    {
        _person.FirstName="Joe";
        _person.LastName="Bloggs";
        return this;
    }

    public PersonBuilder WithValidAddress()
    {
        _person.Address=new Address{
            Address1="1 The Grove",
            PostCode="PembroVille",
            Country="United Kingdom"
        };
        return this;
    }
	
    public PersonBuilder WithInValidAddress()
    {
        _person.Address=null;
        return this;
    }
}

I can build an empty Person

	var person = new PersonBuilder().WithValidDetails().Build();

A Person with valid details and an valid address

var person = new PersonBuilder().WithValidDetails().WithValidAddress().Build();

This is a somewhat simplified example which would work easily well with complex classes.

ZenCoding Visual Studio plugin Overview

The ZenCoding extension for Visual Studio can be installed by itself or as part of the WebEssentials plug-in bundle, see also WebEssentials website. In ZenCoding you use a shorthand to describe a block of html, with the cursor at the end of the line hitting the TAB key will generate the full html code. Getting down to business, here is a list of the formatting characters and their meaning.

#Indicates text defines an element id
>Secified a child element
+A sibling element
.css class(es)
*nElement repeats n times
[]Explicitly specify atrribute.
{xxx}Explicit text content
$Numeric increment

Example 1 – Element Id
So as a first example, type this in to Visual Studio and with the cursor at the end of the line hit the TAB key.

div#sidePanel

And the following HTML is produced

<div id='sidePanel'></div>


That is pretty much all there is to it. For brevity the following examples show the shorthand to type followed by the HTML generated when the TAB key is pressed (when the cursor is at the end of the line).

Example 2 – Explicit Attributes

img[alt='logo image']

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;logo image&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Exampe 3 – Css Class

span.heading

&amp;amp;amp;amp;lt;span class=&amp;amp;amp;amp;quot;heading&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;

For multiple css classes the [] can be used

img[class='logo tooltip-flyout']
or specify each class using the dot notation
img.logo.tooltip-flyout

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;logo tooltip-flyout&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Example 4 – Repeating Elements

ul#features&amp;amp;amp;amp;gt;li*3

&amp;amp;amp;amp;lt;ul id=&amp;amp;amp;amp;quot;features&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul

Example 5 – Explicit Text

label{submit}

&amp;amp;amp;amp;lt;label&amp;amp;amp;amp;gt;submit&amp;amp;amp;amp;lt;/label&amp;amp;amp;amp;gt;

Example 6 – Putting It All Together

div#sidePanel&amp;amp;amp;amp;gt;span.heading+img.logo[alt='logo image']+ul#features&amp;amp;amp;amp;gt;li*3&amp;amp;amp;amp;gt;a{link $}

&amp;amp;amp;amp;lt;div id=&amp;amp;amp;amp;quot;sidePanel&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;span class=&amp;amp;amp;amp;quot;heading&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;logo image&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;logo&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;ul id=&amp;amp;amp;amp;quot;features&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;
        &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;link 1&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
        &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;link 2&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
        &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;link 3&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;

There are a few special features built in, Generation of lorem ipsum text, Lorem Pixel Code and PlaceHold.it blank images.

Example 7 – Generating Lorem Ipsum Text
Here we specify 5 list items each containing 4 words.

ul&amp;amp;amp;amp;gt;li*5&amp;amp;amp;amp;gt;lorem4

&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;Amet, consectetur adipiscing elit.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;Fusce vel sapien elit.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;In malesuada semper mi.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;Id sollicitudin urna fermentum.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;Ut fusce varius nisl.&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;

Example 8 – Lorem Pixel Code
Lorempixel is a free image place holder service. Useful for mocking up layouts where you do not have all your own images yet. Below are a number of Zencoding examples. There are more features like greyscale images and images with text, see the lorempixel.com site for a full list

A completely random image with a default 30 by 30 pixel size.

pix

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/30/30/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

A random image of a specific but equal with and height.

pix-200

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/200/200/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

A random image with different width and height

pix-200x100

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/200/100/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

An image of a specific but equal with and height from a specific category. A random image index will be selected from those in the category, in this case 7, though this can be changed if another image fits your needs. There are generally 20 images per category currently though the odd category may have less than 20 images.

pix-200-animals

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/200/200/animals/7/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Mixing in some of the other Zencoding shorthand. A random image with an ALT attribute and specific CSS class.

pix[alt='product image'].borderless

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://lorempixel.com/30/30/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;product image&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;borderless&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Example 9 – PlaceHold.it Images
Placeholder.com is another image service with similar features. The big difference with this service is the images are grey tone with the image size as text which is quite useful for when you are mocking up layouts containing a number of different image sizes.

A basic image, defaults to 30 by 30 pixels.

place

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://placehold.it/30x30/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

An image with a specific but equal width and height.

place-200

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://placehold.it/200x200/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

An image of a specific but different width and height

place-200x100

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://placehold.it/200x100/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

Mixing in some of the other Zencoding shorthand. A random image with an ALT attribute and specific CSS class.

place[alt='product image'].borderless

&amp;amp;amp;amp;lt;img src=&amp;amp;amp;amp;quot;http://placehold.it/30x30/&amp;amp;amp;amp;quot; alt=&amp;amp;amp;amp;quot;product image&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;borderless&amp;amp;amp;amp;quot; /&amp;amp;amp;amp;gt;

ZenCoding is one of those things where you either love it and tend to use it a lot or don’t see the point in it and so never use it. In the latter case I can see how taking the time to learn and use ZenCoding, which can take a few attempts when trying to create a specific formatted HTML layout, takes more time than just typing (i.e. Copy/Paste) the desired html code in the first place. On the Visual Studio Marketplace ZenCoding pages some people have mentioned how the plug-in does not work in the Community 2017 edition. All the above examples were created using VS2017 Community edition, apart from a few bugs (highlighted below) I can say it does work.

Below are some examples of bugs I have experienced when using Zencoding in MVC views in VS2017 Community edition, although they have worked with Web Forms.

ul&amp;amp;amp;amp;gt;li*3&amp;amp;amp;amp;gt;a

The above produces half a job by formatting the link
ul&amp;amp;amp;amp;gt;li*3&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;content&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;

where I was hoping for an un-ordered list of 3 list items, with each containing a link.
&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;
	&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
	&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
	&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;

Altough in MVC Razor view the following works.

ul&amp;amp;amp;amp;gt;li*3&amp;amp;amp;amp;gt;a.referenceLink

&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;referenceLink&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;referenceLink&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot; class=&amp;amp;amp;amp;quot;referenceLink&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;

Trying to generate a list with explicit text fails but in a different way.

div&amp;amp;amp;amp;gt;ul&amp;amp;amp;amp;gt;li*3{abc}

&amp;amp;amp;amp;lt;div&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;

For me, the Zencoding plug in is a simple tool that enhances my coding time.

Using EntityFramework 6 Part 2: Database Connections

This post is part of a mini series on using EntityFramework (EF). If you do not have EF installed go to Part 1: Introduction and Setup to see what (basic) steps we took to get EF installed.
In this part we are going to use Code-First methods to start model building, then eventually connecting to the database . This is not the only way and you do not have to do things in exactly the same order as I have but if you follow along you should have a working version at the end.

Database
Firstly, create a database at your preferred location. Once you have a database add the connection string to the web.config file. I added a database to the App_Data folder so this is what the connectionsString definition look like in my web.config file.

  &lt;connectionStrings&gt;
    &lt;add name=&quot;DBConn&quot; connectionString=&quot;Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB.mdf;Integrated Security=True&quot;
         providerName=&quot;System.Data.SqlClient&quot; /&gt;
  &lt;/connectionStrings&gt;

Next, add a class which inherits from DBContext. An instance of DBContext, LibraryDBContext in this example, is used to perform CRUD operations against a database. To read up on DBContext see these articles
http://www.entityframeworktutorial.net/EntityFramework4.3/dbcontext-vs-objectcontext.aspx

namespace WebApplication2.Models
{
    public class LibraryDBContext:DbContext
    {
        public LibraryDBContext() : base(&quot;DBConn&quot;)
        { }

        public DbSet&lt;Book&gt;Books{ get; set; }
    }
}

This gives sufficient Entity Framework code to enable our application to access a database.

The Model
Now we will start the model by creating a Book entity. Create a Book class in the Models folder of your application.

namespace WebApplication2.Models
{
    public class Book
    {
        public int BookId { get; set; }
        public string Author { get; set; }
        public string Title { get; set; }
    }
}

Following Code-First conventions, because we have defined an property called BookId (class name ‘Book’ plus ‘Id’) Entity Framework will nominate this property as an identity column when the database tables are generated. If we had a property simply named Id, Entity Framework would have nominated that as the identity and created an identity column called Id.

Displaying Book Info
Add a Controller to the application, I have called it BooksController. The Index action retrieves all the books from what will be the Books table.

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class BooksController : Controller
    {
          public ActionResult Index()
        {
            var books = new List&lt;Book&gt;();
            using (var db = new LibraryDBContext())
            {
                books = db.Books.ToList();
            }
            return View(books);
        }
    }
}

The last code related change is to create a View for the Index Action to display all the books.

@model IEnumerable&lt;WebApplication2.Models.Book&gt;
    @Html.ActionLink(&quot;Create New&quot;, &quot;Create&quot;)
&lt;table class=&quot;table&quot;&gt;
&lt;tr&gt;
&lt;th&gt;
            @Html.DisplayNameFor(model =&gt; model.Author)&lt;/th&gt;
&lt;th&gt;
            @Html.DisplayNameFor(model =&gt; model.Title)&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
@foreach (var item in Model) {
&lt;tr&gt;
&lt;td&gt;
            @Html.DisplayFor(modelItem =&gt; item.Author)&lt;/td&gt;
&lt;td&gt;
            @Html.DisplayFor(modelItem =&gt; item.Title)&lt;/td&gt;
&lt;td&gt;
            @Html.ActionLink(&quot;Edit&quot;, &quot;Edit&quot;, new { id=item.BookId }) |
            @Html.ActionLink(&quot;Details&quot;, &quot;Details&quot;, new { id=item.BookId }) |
            @Html.ActionLink(&quot;Delete&quot;, &quot;Delete&quot;, new { id=item.BookId })&lt;/td&gt;
&lt;/tr&gt;
}&lt;/table&gt;

Running the Application
The application should build at this point. Currently we have a database that contains no tables. If you run the application now The database/tables will only be created/modified on a read/write action. So run the application and navigate to your controller (e.g. \books). Because the Index action of the Books controller contains code that retrieves data via the inherited DBContext class, and because Entity Framework knows there are models with no corresponding database table, the tables will be created with columns types that match the model properties.