MVC Scaffolding not showing fields on view in EF Core 2.0

asp.net asp.net-core asp.net-mvc c# entity-framework-core

Question

I am currently trying to learn .NET core 2.0 and ASP.NET and doing a code-first approach on a project.

The problem is that the view (/Packages/) is not showing the categories, even though they are marked as [Required] at the class, the Scaffolding is somehow ignoring it and just creating the view without it.

This is a big problem because if you try to edit or add a new entry, it will just hang in there as Category is a required field in the DB. Needless to say, it is also not showing the list of Products as well... What am I missing?

Package.CS

public class Package
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int Price { get; set; }
    [Required] public virtual Category Category { get; set; }
    public virtual ICollection<Product> Products { get; set; }

}

Category.CS

public class Category
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Product.CS

public class Product
{
    public int ID { get; set; }
    public string SKU_ID { get; set; }
    public Category Category { get; set; }
    public int Price { get; set; }
    public int Cost { get; set; }

    public List<Variation> Variations { get; set; }
}

DatabaseContext.CS

public class DatabaseContext : DbContext
{
    public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
    {
    }

    public DbSet<Brand> Brands { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Combo> Combos { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Package> Packages{ get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Variation> Variations { get; set; } 
}

I've even tried with and without

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    #region Package -> Product Relationship
    modelBuilder.Entity<Package>()
        .HasMany(x => x.Products);

    modelBuilder.Entity<Package>()
       .HasOne(xa => xa.Category);
    #endregion
}

This is how the View Looks:

View At Packages

1
0
5/9/2018 6:39:14 PM

Accepted Answer

Alright, to answer my own question and in hopes that if somebody new like me encounters the same issue, knows how to fix it.

Turns out that I was not properly implementing the relationship on my model, so Entity Framework was unsure if it was really a relationship or what exactly it was...

So in short, you must let all the classes that interact together know that their field will be used and by whom.

Package.CS

public class Package
    {
        public int PackageId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public int Price { get; set; }
        public int CategoryID { get; set; }


        [Required]
        public virtual Category Category { get; set; }
        public virtual ICollection<Product> Products { get; set; }

    }

Please, note that you also have to let the model you intend to use your other class know what field is the key. In my case:

public int CategoryID { get; set; }

Category.CS

public class Category
    {
        public int CategoryId { get; set; }
        [Required]
        public string CategoryName { get; set; }

        public virtual ICollection<Package> Packages { get; set; }
    }

And pretty much those are all the changes I had to do on my model.

If, for any reason, you just get the ID of your table to show on the SelectView, don't panic. Just means you forgot to mark as [Required] the text field. [you cannot allow nulls]

0
10/23/2017 7:45:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow