I created two classes in the Models folder, one for Products and one for Product Groups, modeled exactly on Albums and Genres in the book, where Products contains a Product Group element and both Products and Product Groups are identified by an "Id" element. I then generated the database using the "MVC Controller with Views, Using EF" option, using "Products" as the Model class and creating a data context class as described in the book.
This worked as predicted and expected on the database side. A database is created with the two tables linked properly. One can see and use it in the "Data Connections" server explorer panel. But the controller generated for products, and the corresponding views, do not generate linked ability to view or edit the product group for the products.
Here are my two classes, both separate classes in the "Models" folder:
Code:
public class Product
{
public virtual int ProductId { get; set; }
public virtual string ProductName { get; set; }
public virtual ProductGroup ProductGroup { get; set; }
}
Code:
public class ProductGroup
{
public virtual int ProductGroupId { get; set; }
public virtual string GroupName { get; set; }
public virtual List<Product> Products { get; set; }
}
Here is the properly generated db cs (which, by the way, only added the product group line after creating controllers for both products and product groups):
Code:
public class MVCTestModelsContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
public MVCTestModelsContext() : base("name=MVCTestModelsContext")
{
}
public System.Data.Entity.DbSet<MVCTestModels.Models.Product> Products { get; set; }
public System.Data.Entity.DbSet<MVCTestModels.Models.ProductGroup> ProductGroups { get; set; }
}
But the code does not generate linked views. Here, for example, is the relevant portion of the "Index" view generated for the "Products" controller:
Code:
<div class="form-horizontal">
<h4>Product</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ProductId)
<div class="form-group">
@Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
Note that there is no column for product group. Similarly, in the edit view, no drop down list for product group is created.
What may be causing this? I tried to follow the instructions on generating the database and corresponding controllers and views to the T.