Q:

Handling Custom Fields using the Entity Framework

Currently I have a project that needs to handle products with custom fields that the users can specify.
Does anyone know what the best way to implement this using Models and Code First Annotations?
I figured on the database side, I will need a new table to handle custom data that will be linked to the productID.

A:

You can specify a seperate table for custom properties that your users can add. This custom field will have a reference to your main model. Basically, you will have 1 to M relationship
public class MyModel
{
    public int MyModelID            { get; set; }

    public string FixedProperty1    { get; set; }
    public string FixedProperty2    { get; set; }

    // This is a navigation property for all your custom properties
    public virtual ICollection<CustomProperty> CustomProperties  { get; set; }
}

public class CustomProperty
{
    public int CustomPropertyID      { get; set; }

    // This is the name of custom field
    public string PropertyName       { get; set; }
    // And this is its value
    public string PropertyValue      { get; set; }

    // FK reference and navigation property to your main table
    public int MyModelID             { get; set; }
    public virtual MyModel MyModel   { get; set; }
}

