简体   繁体   English

有没有办法遍历EF6实体?

[英]Is there a way to iterate through an EF6 Entity?

I have a web form with around 50 fields that is used for crud operations on an Oracle DB, I am using EF6. 我有一个包含大约50个字段的Web表单,用于在Oracle DB上执行Crud操作,我正在使用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. 其中,GENERIC_FTP_SEND是我的Model.context.cs中虚拟DbSet的名称。

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. 我要完成的工作是提供一些代码,使我可以遍历ftpPartner的属性并将它们与字段ID进行比较以进行匹配。 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 如果您需要/想要在这里看到它是我的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 实体框架6:有没有一种方法可以遍历表而无需在内存中保留每一行

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);
    }
}

You can also use the Entity Framework context object as well to accomplish the same if you know you are going to insert. 如果您知道要插入,则也可以使用Entity Framework上下文对象来完成相同的操作。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM