简体   繁体   English

在实体框架中按名称获取字段

[英]Get field by name in Entity Framework

What would be the easiest and least labour intensive (from the software POV) for me to be able to get a property (to be read and modified) from an entity generated via the Entity Framework? 对于我来说,从通过实体框架生成的实体获取属性(进行读取和修改)的最简单,最省力的工作是什么?

Example (and simplified code) below: 下面的示例(和简化的代码):

// <auto-generated> from Entity Framework
public partial class tblCustomer
{
    public int CustomerID { get; set; }
    public string Status { get; set; }
}

For instance I would like: 例如,我想要:

tblCustomer customer = new tblCustomer();
int pCustomerID = customer.GetFieldByName("CustomerID");
pCustomerID = 100;

I've read a lot of answers about Reflection, but also comments that it's heavy processing (may be a bit excessive for my needs). 我已经阅读了很多有关Reflection的答案,但也评论说这很繁琐(可能对我的需求而言有点过分了)。

My example code may seem rather simplistic (so a solution would be rather pointless for that, the real code is based on large tables), but if there was some sort of GetFieldByName function I could reduce my code significantly as I have a lot of repetitious code doing the same stuff (just to different columns). 我的示例代码可能看起来很简单(因此,解决方案将毫无意义,实际代码是基于大型表的),但是如果有某种GetFieldByName函数,我可以大大减少代码,因为我有很多重复代码做同样的事情(只是针对不同的列)。

If I understand your problem correctly, I think you can use the changetracker for this (if the entity is in the context already). 如果我正确理解了您的问题,我认为您可以为此使用changetracker(如果实体已经在上下文中)。

dbContext.Entry(customer).CurrentValues["CustomerID"]

will give you the value of CustomerID for the customer object, provided it is attached to the dbContext instance. 将为您提供customer对象的CustomerID值,前提是该对象附加到dbContext实例。

If it is not part of the context, you can use Attach() to attach it first, or use Add() , if it's supposed to be a new record. 如果它不是上下文的一部分,则可以使用Attach()首先将其附加,如果应该是新记录,则可以使用Add()

If you don't like to use Reflection the only way that i know is using a dictionary in your entities and also you can put all these stuff in a base class and your entities inherit it for example like that: 如果您不喜欢使用反射,那么我所知道的唯一方法就是在您的实体中使用字典,并且您也可以将所有这些内容放入基类中,然后您的实体就可以像这样继承它:

     [Serializable]
public class BaseEntity 
{
    Dictionary<string, object> _dic;

    public BaseEntity()
    {
        _dic = new Dictionary<string, object>();
    }

    public object this[string propertyName]
    {
        get
        {
            return _dic[propertyName];
        }
        set
        {
            _dic[propertyName] = value;
        }
    }      
}

public class tblCustomer : BaseEntity
{
    public int CustomerID
    {
        get
        {
            return (int)this["CustomerID"];
        }
        set
        {
            this["CustomerID"] = value;
        }
    }
    public string Status
    {
        get
        {
            return (string)this["Status"];
        }
        set
        {
            this["Status"] = value;
        }
    }
}

tblCustomer customer = new tblCustomer(); tblCustomer客户=新的tblCustomer(); int pCustomerID = customer["CustomerID"]; int pCustomerID = customer [“ CustomerID”];

and about performance cost of Reflection you can for first time store your memberInfos in a static field and use it for all instances. 以及关于Reflection的性能成本,您可以第一次将memberInfos存储在静态字段中,并将其用于所有实例。

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

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