c# - How would I add properties to an entity using a method instead of hard coding it (using Entity Framework)? -


i using entity framework store entities have coded in c# create simple web application. want know how add property (field) entity without coding entity class. take employee entity, example:

public class employee : person {     public int officeid { get; set; }     public datetime hiredate { get; set; }     public string jobtitle { get; set; }     public int salary { get; set; }      public virtual office office { get; set; } } 

now let's want employee entity have

string certifications  

property describes employees certifications. write below line define salary.

however, need able implement functionality user can add property using web application, means can't hardcoded. i'm imagining there "add property" button, , when user clicks it, have to:

  • select entity want add property (e.g. employee entity)
  • write name of property type (e.g. certifications, string)
  • write value of property (e.g. "officially certified ok programmer")

i new of this, bear me, it's not intuitive me write method takes in user input entity, property name , type, , value of property, add entity make property other properties hardcoded in entity class. appreciate if has suggestions on how go persisting properties added @ run time , linking them entity function if coded in entity class original properties.

i have method allows user edit value of 1 of properties of "office" entity (credit tom dykstra's entity framework 6 tutorial @ asp.net):

[httppost, actionname("edit")] [validateantiforgerytoken] public actionresult editpost(int? id) {     if (id == null)     {         return new httpstatuscoderesult(httpstatuscode.badrequest);     }     var officetoupdate = db.offices.find(id);     if (tryupdatemodel(officetoupdate, "",        new string[] { "address", "businessname" }))     {         try         {            db.savechanges();             return redirecttoaction("index");         }         catch (dataexception /* dex */)         {             //log error (uncomment dex variable name , add line here write log.             modelstate.addmodelerror("", "unable save changes. try again, , if problem persists, see system administrator.");         }     }     return view(officetoupdate); } 

however instead of editing value of 1 of properties, asking how edit entity whole new property + value added. hope post clear enough.

i don't see how possible in exact way describe it.

if needed have sort of "extensible" entity requirement, users can dynamically add new properties, create couple of generic tables + entities exact purpose. , this:

public class extraentityproperty {     public int id { get; set; } // pk     public string entityname { get; set; }     public string propertytype { get; set; }     public string propertyname { get; set; } }  public class extraentitypropertyvalue {     public int id { get; set; } // pk     public int extraentitypropertyid { get; set; } // fk     public string propertyvalue { get; set; } } 

...and normalized bit more, depending on how want work it.

the point here that, way, deal purely in terms of data, ef can dynamically. if users need add new properties dynamically, add new rows extraentityproperty table. , if users assign new values properties, add rows extraentitypropertyvalue.

personally, not big fan of these generic solutions, because lose lot of type safety otherwise if can statically pre-define types , properties ahead of time. if need dynamic, maybe can take idea , make work you.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -