0

I am developing a CMS and I have a category class like this :

public partial class Category
{
   public int CategoryId {get;set;}
   public string CategoryName {get;set;}
   public string CategoryType {get;set;}
}

I am using Entity Framework to store & create such database table in my database. Now, how can I add dynamic fields to this model and hence to the database. I am allowing the user to add his desired custom fields to be added. So, my database schema becomes

Category(CategoryId, CategoryName, CategoryType, CustomField1, CustomField2);

How can I do this using EF 5?

1 Answer 1

3

You can't in the way you expect it. Instead of adding custom fields to Category table you need something like CategoryExtension table where you will store CategoryId, FieldName and FieldValue (key-value pairs related to specific category record) where CategoryId will be FK to Category table and for example CategoryId and FieldName will build composite PK. Your classes will then look like:

public partial class Category
{
   public int CategoryId {get;set;}
   public string CategoryName {get;set;}
   public string CategoryType {get;set;}
   public virtual ICollection<CategoryExtension> Extensions { get; set; }
}

public class CategoryExtension 
{
   public int CategoryId { get; set; }
   public string Name { get; set; }
   public string Value { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.