How to add database field in table dynamically using MVC and Entity Framework using .Net?

asp.net-mvc, c#, database, entity-framework, model-view-controller

Solution

Going to add this suggestion as an answer, as I don't think dynamically adding columns to the database is the best idea.

In your `Category` class, add in a list of another type, lets call it `CustomField`. When you allow a user to add a new field, just stick it in this list

public partial class Category
{
   public int CategoryId {get;set;}
   public string CategoryName {get;set;}
   public IList<CustomField> CustomFields {get;set;}
}

public class CustomField
{
   public int CustomFieldId {get;set;}

   public string FieldName {get;set;}
   public string FieldValue {get;set;}

   [ForeignKey("Category")]
   public int CategoryId {get;set;}
   public Category Category {get;set;}
}

Problem

I am developing a .Net application and I have Database available. I have a category class like : ``` public partial class Category { public int CategoryId {get;set;} public string CategoryName {get;set;} } ``` How can I add dynamic fields to this model and hence to the database? if not possible then is there any other way to fulfill my problem? I am allowing the user to add his desired custom fields to be added. So, my database schema becomes Category(CategoryId, CategoryName, CustomField1, CustomField2); How can I do this using EF 5 and MVC ? Or is there any other way we can do it ?

Original source