Search code examples
c#asp.net-mvc-4entity-framework-5asp.net-mvc-scaffolding

MvcScaffolding and Hinting to find Enums and related classes


So I've got a Deeply connected POCO that I've written using EF5/Code First, it has one-to-one, and one-to-many relationships with a number of classes and Enum properties for things like Gender, AddressType and so on.

A very simplified view:

public class Contact
{
   public string FullName { get; set; }

   public string Title { get; set;

   public GenderEnum Gender { get; set; }

   public Country CountryOfBirth { get; set; }

   public string Address { get; set; }
}

I've got a DbContext for this Object and the others, and it also creates a lot of Seed data for the Country list and the various other things I need to represent.

However, when I come to use MvcScaffolding like so:

Scaffold Controller Model.Contact -Repository -DbContext Model.Context

It creates a _CreateOrEdit.cshtml view that shows the String fields from my Contact model, but doesn't provide a drop down list for the Enums, or any of the related classes.

So, How do I tell MvcScaffolding that the DataType for a specific property can be loaded from the Context, or do I need to do this manually ?


Solution

  • Actually turned out to be a lot simpler.

    What I'd done was just use Class relationships and whilst Entity Framework was happy enough with that, MvcScaffolding didn't recognize the relationships.

    Once I'd added a backing Id field and a ForeignKey attribute to the actual type, MvcScaffolding picked it straight up.