C# 使用实体框架以JSON的形式返回类,并在API中附加一个字段

C# 使用实体框架以JSON的形式返回类,并在API中附加一个字段,c#,json,object,.net-core,dbcontext,C#,Json,Object,.net Core,Dbcontext,我有一个类Employee,其中包含许多属性,当我想从数据库中读取它并返回它的列表时。我想给它添加一个额外的字段 现在是这样的, return await _context.Employees.ToListAsync(); public class CustomEmployee<T> : Employee { public CustomEmployee() { } //for cases if you don't want to copy values while cre

我有一个类
Employee
,其中包含许多属性,当我想从数据库中读取它并返回它的列表时。我想给它添加一个额外的字段

现在是这样的,

return await _context.Employees.ToListAsync();
public class CustomEmployee<T> : Employee
{

    public CustomEmployee() { } //for cases if you don't want to copy values while creating an instance

    public CustomEmployee(Employee employee)
    {
        foreach (PropertyInfo prop in employee.GetType().GetProperties())
        {
            PropertyInfo prop2 = employee.GetType().GetProperty(prop.Name);
            prop2.SetValue(this, prop.GetValue(employee, null), null);
        }
    }

    public T Data { get; set; }
}
return await _context.Employees.Select(em => new CustomEmployee(em)<string> 
{ 
    Data = "Extra data" 
}).ToListAsync();
我不想在
Employee
之后使用
Select
命令,因为据我所知,我必须在
Employee
类中列出我的所有属性才能添加一个额外的属性,如下所示:

return await _context.Employees.Select(em => new 
{
   em.Firstname, // In employee class
   em.Lastname, // In employee class
   ... // Other properties in employee class
   EXTRAFIELD = "Data" // Extra field with any data I want to add to employee
}).ToListAsync();
但是在上面的解决方案中,我必须列出employee类中的所有字段

我不想执行以下操作,因为它会将employee放入一个新的对象名称em中:

return await _context.Employees.Select(em => new 
    {
       em,
       EXTRAFIELD = "Data" // Extra field with any data I want to add to employee
    }).ToListAsync();

有没有办法告诉
DbContext
\u context
)返回
Employee
字段并在其中添加“this new field”(创建与
Employee
具有相同属性的新对象,只需一个额外属性)?

如何创建并返回一个将继承Employee属性的自定义对象?您可以添加额外的通用字段,例如:

public class CustomEmployee<T> : Employee
{
    public T Data { get; set; }
}
公共类CustomEmployee:Employee
{
公共T数据{get;set;}
}
我知道您不想使用选择将值从一个对象复制到另一个对象,但我们可以创建如下内容:

return await _context.Employees.ToListAsync();
public class CustomEmployee<T> : Employee
{

    public CustomEmployee() { } //for cases if you don't want to copy values while creating an instance

    public CustomEmployee(Employee employee)
    {
        foreach (PropertyInfo prop in employee.GetType().GetProperties())
        {
            PropertyInfo prop2 = employee.GetType().GetProperty(prop.Name);
            prop2.SetValue(this, prop.GetValue(employee, null), null);
        }
    }

    public T Data { get; set; }
}
return await _context.Employees.Select(em => new CustomEmployee(em)<string> 
{ 
    Data = "Extra data" 
}).ToListAsync();
公共类CustomEmployee:Employee
{
public CustomEmployee(){}//用于在创建实例时不希望复制值的情况
公共海关雇员(雇员)
{
foreach(employee.GetType().GetProperties()中的PropertyInfo属性)
{
PropertyInfo prop2=employee.GetType().GetProperty(prop.Name);
prop2.SetValue(this,prop.GetValue(employee,null),null);
}
}
公共T数据{get;set;}
}
在构造函数中,您必须传递一个继承的实例Employee,上面的代码将值从Employee复制到CustomEmployee

然后像这样使用:

return await _context.Employees.ToListAsync();
public class CustomEmployee<T> : Employee
{

    public CustomEmployee() { } //for cases if you don't want to copy values while creating an instance

    public CustomEmployee(Employee employee)
    {
        foreach (PropertyInfo prop in employee.GetType().GetProperties())
        {
            PropertyInfo prop2 = employee.GetType().GetProperty(prop.Name);
            prop2.SetValue(this, prop.GetValue(employee, null), null);
        }
    }

    public T Data { get; set; }
}
return await _context.Employees.Select(em => new CustomEmployee(em)<string> 
{ 
    Data = "Extra data" 
}).ToListAsync();
return await\u context.Employees.Select(em=>newcustomemployeen(em)
{ 
Data=“额外数据”
}).ToListAsync();

因此,在当前的
Employee
模型中尚未定义额外字段?另外,您使用的ORM是什么?是不是
实体框架
?它的工作原理就是这样改变的:if(prop2.CanWrite)prop2.SetValue(This,prop.GetValue(employee,null),null);