简体   繁体   English

使用实体框架将 class 作为 JSON 返回 API 中的一个额外字段

[英]Return class as JSON with one extra field in API using Entity Framework

I have a class Employee with many properties in it and when I want to read it from the database and return a list of it.我有一个 class Employee ,其中有许多属性,当我想从数据库中读取它并返回它的列表时。 I want to add an extra field to it.我想为它添加一个额外的字段。

Currently it's like this:目前是这样的:

return await _context.Employees.ToListAsync();

I don't want to use Select command after Employee because as far as I know I have to list all my properties in the Employee class to be able to add an extra one like below:我不想在Employee之后使用Select命令,因为据我所知,我必须在Employee class 中列出我的所有属性才能添加一个额外的属性,如下所示:

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

But in the solution above I have to list all my fields in employee class.但是在上面的解决方案中,我必须在员工 class 中列出我的所有字段。

I don't want to do as below as it puts employee in a new object names em:我不想做下面的事情,因为它把员工放在一个新的 object 名称中:

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

Is there a way to tell DbContext ( _context ) to return Employee fields and add "this new field" in it (create new object which has same properties as Employee with one extra)?有没有办法告诉DbContext ( _context ) 返回Employee字段并在其中添加“这个新字段”(创建新的 object ,它与Employee具有相同的属性,但多了一个)?

How about to create and return a custom object that will inherit Employee properties?如何创建并返回将继承 Employee 属性的自定义 object? And you can add extra generic field, for example:您可以添加额外的通用字段,例如:

public class CustomEmployee<T> : Employee
{
    public T Data { get; set; }
}

I know you don't want to copy values from object to object with Select , but we can create something like this:我知道您不想使用 Select 将值从 object 复制到object ,但我们可以创建如下内容:

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

In constructor you have to pass an inherited instance Employee, and code above will copy values from Employee to CustomEmployee.在构造函数中,您必须传递一个继承的实例 Employee,上面的代码会将值从 Employee 复制到 CustomEmployee。

And use like this:并像这样使用:

return await _context.Employees.Select(em => new CustomEmployee(em)<string> 
{ 
    Data = "Extra data" 
}).ToListAsync();

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

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