dimanche 28 juin 2015

.NET tutorial cant understand purpose of certain fields in .cs Model files

I have been following the a tutorial on ASP.NET - http://ift.tt/1edRtVj

They start with building the data layer (the application is a shopping website for toys where each item belongs to one of four categories). So, they start with building out the "products" and "categories" tables. The products table has the following code -

[ScaffoldColumn(false)]
public int ProductID { get; set; }

[Required, StringLength(100), Display(Name = "Name")]
public string ProductName { get; set; }

[Required, StringLength(10000), Display(Name = "Product Description"), DataType(DataType.MultilineText)]
public string Description { get; set; }

public string ImagePath { get; set; }

[Display(Name = "Price")]
public double? UnitPrice { get; set; }

public int? CategoryID { get; set; }

public virtual Category Category { get; set; }

In the code above, I don't understand what the purpose of the last field,

 public virtual Category Category { get; set; }

is.

And then similarly the categories .cs file has the following code -

[ScaffoldColumn(false)]
public int CategoryID { get; set; }

[Required, StringLength(100), Display(Name = "Name")]
public string CategoryName { get; set; }

[Display(Name = "Product Description")]
public string Description { get; set; }

public virtual ICollection<Product> Products { get; set; }

Again, what is the purpose of

public virtual ICollection<Product> Products { get; set; }

And then finally, there is a context class with the following code -

using System.Data.Entity;
namespace WingtipToys.Models
{
  public class ProductContext : DbContext
  {
    public ProductContext() : base("WingtipToys")
  {
}
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
  }
}

This one has a field called Products but so did the second .cs file. So, what is this one for and won't it conflict with the other "Products" object which was also global?

Aucun commentaire:

Enregistrer un commentaire