STEPS
1) Open your MvcMovie solution
2) Adding Model Classes
2-1) In Solution Explorer, right click the Models folder, select Add, and then select Class.
2-2) Enter the class name "Movie".
2-3) Look at the codes.
2-4) Delete All Codes
2-5) Create a namespace (a container for group of codes)
2-6) Add Movie Class
2-7) Add EF database context
3) Define Database Connection
This tutorial is based on http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model
Continue from the previous tutorial, http://visualstudio-steps.blogspot.com/2014/09/getting-started-with-asp-net-mvc-4-part_68.html .
STEPS
1) Open your MvcMovie solution
2) Adding Model Classes
2-1) In Solution Explorer, right click the Models folder, select Add, and then select Class.
2-2) Enter the class name "Movie".
2-3) Look at the codes.
Info:
At this point, the codes that have been created for us are fine. However, for learning purpose, we are going to clear them all and start coding from scratch.
|
2-4) Delete All Codes
2-5) Create a namespace (a container for group of codes)
namespace MvcMovie.Models
{
}
|
2-6) Add Movie Class
namespace MvcMovie.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
}
|
2-7) Add EF database context
namespace MvcMovie.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
}
|
Info:
We get error indicators for the keywords that are not recognized by the IDE. We need to add references for these keywords.
|
2-8) Add reference to System Library (to fix error on line no.7 above)
Using System;
namespace MvcMovie.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
}
|
2-8) Add reference to DbContext and DbSet (to fix error on line no. 11 and 13)
Using System;
using System.Data.Entity;
namespace MvcMovie.Models
{
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}
}
|
3) Define Database Connection
Info:
By default, a database connection has been specified by the IDE. You can check this in the Web.config file.
However if you want to define your own connection, you can add codes as shown below specifying a connection string for “MovieDBContext”
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcMovie-2012213181139;Integrated Security=true"
providerName="System.Data.SqlClient"
/>
<add name="MovieDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
|
---
No comments:
Post a Comment