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

C# 在实体框架中按名称获取字段,c#,.net,entity-framework,C#,.net,Entity Framework,对于我来说,从通过实体框架生成的实体中获取属性(要读取和修改)最简单、劳动强度最低的方法(从软件POV)是什么 示例(和简化代码)如下: 我已经阅读了很多关于反思的答案,但也评论说这是一个繁重的过程(对于我的需求来说可能有点过度) 我的示例代码可能看起来相当简单(因此解决方案对此毫无意义,真正的代码是基于大型表的),但如果有某种GetFieldByName函数,我可以显著减少代码,因为我有很多重复代码在做相同的事情(只是针对不同的列).如果我正确理解您的问题,我认为您可以使用changetrac

对于我来说,从通过实体框架生成的实体中获取属性(要读取和修改)最简单、劳动强度最低的方法(从软件POV)是什么

示例(和简化代码)如下:

我已经阅读了很多关于反思的答案,但也评论说这是一个繁重的过程(对于我的需求来说可能有点过度)


我的示例代码可能看起来相当简单(因此解决方案对此毫无意义,真正的代码是基于大型表的),但如果有某种GetFieldByName函数,我可以显著减少代码,因为我有很多重复代码在做相同的事情(只是针对不同的列).

如果我正确理解您的问题,我认为您可以使用changetracker(如果实体已经在上下文中)

dbContext.Entry(customer).CurrentValues[“CustomerID”]

将为
customer
对象提供
CustomerID
的值,前提是该对象附加到
dbContext
实例


如果它不是上下文的一部分,可以使用
Attach()
先附加它,或者使用
Add()
,如果它应该是一个新记录。

如果您不喜欢使用反射,我知道的唯一方法是在您的实体中使用字典,并且您可以将所有这些内容放在基类中,您的实体继承它,例如:

     [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;
        }
    }
}
[可序列化]
公共类基实体
{
字典;
公共基础实体()
{
_dic=新字典();
}
公共对象此[string propertyName]
{
得到
{
返回[propertyName];
}
设置
{
_dic[propertyName]=值;
}
}      
}
公共类TBL客户:BaseEntity
{
公共int客户ID
{
得到
{
返回(int)此[“CustomerID”];
}
设置
{
此[“CustomerID”]=值;
}
}
公共字符串状态
{
得到
{
返回(字符串)此[“状态”];
}
设置
{
此[“状态”]=值;
}
}
}
tblCustomer customer=新tblCustomer(); int pcCustomerId=customer[“CustomerID”]


关于反射的性能成本,您可以第一次将您的memberInfos存储在静态字段中,并在所有实例中使用它。

是的,字典似乎是一种可行的方法,而且我不需要预先使用反射,因为它只需要做一次。由于字典存储了这些值的副本,我假设我必须为所有属性设置变异符(以便像上面那样不断更新条目),因此必须修改tt文件,使其以上述格式创建类。
tblCustomer customer = new tblCustomer();
int pCustomerID = customer.GetFieldByName("CustomerID");
pCustomerID = 100;
     [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;
        }
    }
}