-1

I have a web form with around 50 fields that is used for crud operations on an Oracle DB, I am using EF6. Currently, I accomplish this like so:

private GENERIC_FTP_SEND GetFields()
{
    GENERIC_FTP_SEND ftpPartner = new GENERIC_FTP_SEND();

    //Contact Info
    ftpPartner.FTP_LOOKUP_ID = FTP_LOOKUP_IDTB.Text;
    ftpPartner.PARTNER_NAME = PARTNER_NAMETB.Text;
    ftpPartner.REMEDY_QUEUE = REMEDY_QUEUETB.Text;
    ftpPartner.PRIORITY = PRIORITYBtns.SelectedValue;
    ftpPartner.CONTACT_EMAIL = CONTACT_EMAILTB.Text;
    ftpPartner.CONTACT_NAME = CONTACT_NAMETB.Text;
    ftpPartner.CONTACT_PHONE = CONTACT_PHONETB.Text;
    ...
}

where GENERIC_FTP_SEND is the name of the virtual DbSet in my Model.context.cs.

This works fine but is not reusable in the least. What I would like to accomplish is to have some code that allows me to iterate through the attributes of ftpPartner and compare them to the field id for a match. Something like this:

var n =0;
foreach (Control cntrl in ControlList){
    if(cntrl.ID == ftpPartner[n]){
         ftpPartner[n] = cntrl.Text;
    }
    n++;
}

In case you need/want to see it here is my Model.context.cs

public partial class Entities : DbContext{

public Entities(): base("name=Entities")
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}

public virtual DbSet<GENERIC_FTP_SEND> GENERIC_FTP_SEND { get; set; }

}

I saw the question here but I am not sure how to implement that in my case. Entity Framework 6: is there a way to iterate through a table without holding each row in memory

Community
  • 1
  • 1
  • With Reflection maybe http://stackoverflow.com/questions/8151888/c-sharp-iterate-through-class-properties – Jasen Jan 26 '16 at 17:39

2 Answers2

2

You can achieve that with reflection:

var type = typeof(GENERIC_FTP_SEND);
foreach (Control cntrl in ControlList){
    Object value = null;


    if (cntrl is TextBox){
         value = (cntrl as TextBox).Text;
    } else if (cntrl is GroupBox){
         value = (cntrl as GroupBox).SelectedValue;
    } //etc ...



    PropertyInfo pInfo = type.GetProperty(cntrl.ID);
    if (pInfo != null && value != null){
        pInfo.SetValue(ftpPartner, value, null);
    }
}
serhiyb
  • 4,753
  • 2
  • 15
  • 24
  • The Tag (Or maybe name) property would be a good place to store the field name. With this in mind, it'd be pretty trivial to build the form this is shown on from the data type as well. – Nick Jan 26 '16 at 18:58
  • Works great and does exactly what I needed. – Charles Driver Jr. Jan 27 '16 at 14:38
  • @Nick I'm not sure what you mean about being trivial. – Charles Driver Jr. Jan 27 '16 at 14:39
  • Pretty much the opposite of the above. Instead of pulling the values out of the Controls collection, you put them into the controls collection in the constructor. Make a label with the property name, and a data entry control with the corresponding data type for each property. – Nick Feb 01 '16 at 19:03
0

You can also use the Entity Framework context object as well to accomplish the same if you know you are going to insert.

var x = new GENERIC_FTP_SEND();

//  Add it to your context immediately
ctx.GENERIC_FTP_SEND.Add(x);

//  Then something along these lines 
foreach (Control cntrl in ControlList)
{
    ctx.Entry(x).Property(cntrl.Name).CurrentValue = ctrl.Text;
}
Bubba
  • 275
  • 1
  • 10