Servicestack Ormlite 自动映射 [英] Servicestack Ormlite Automapping

查看:58
本文介绍了Servicestack Ormlite 自动映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次开始使用 ServiceStack 和 OrmLite,并让自己陷入了困境.

I have started using ServiceStack and OrmLite for the first time and have got myself in a bit of a pickle.

我已经构建了 2 个类,1 个用于获取输入参数,1 个用于保存响应.

I have built 2 classes, 1 is to take the input parameters and 1 is to hold the response.

他们在这里...

[DataContract]
public class EMEM
{
    public int EMCo { get; set; }
    public string Location { get; set; }
    public string Department{ get; set; }


    [DataMember]
    public string Equipment { get; set; }
    [DataMember]
    public string udDriver { get; set; }
    [DataMember]
    public string udDrillRigCrew { get; set; }
    [DataMember]
    public DateTime udDOTInspectDate { get; set; }
    [DataMember]
    public string udDOTInspectReq { get; set; }
    [DataMember]
    public DateTime udDOTInspectExpire { get; set; }
}

public class EMEMResponse
{
    public int EMCo { get; set; }
    public string Location { get; set; }
    public string Department{ get; set; }
    public string Equipment { get; set; }
    public string udDriver { get; set; }
    public string udDrillRigCrew { get; set; }
    public DateTime udDOTInspectDate { get; set; }
    public string udDOTInspectReq { get; set; }
    public DateTime udDOTInspectExpire { get; set; }
    public string extraField1 { get; set;}
    public string extraField2 { get; set;}
}

目前我有以下代码调用数据库:

At the moment I have the following code calling the database:

public object Get(EMEM request)
    {
        var dbFactory = new OrmLiteConnectionFactory(Global.connString, SqlServerDialect.Provider);
        using (IDbConnection db = dbFactory.OpenDbConnection())
        {
            if (request.Equipment != null)
            {
                List<EMEM> results = db.Select<EMEM>(p => p.Where(ev => ev.Equipment == request.Equipment && ev.EMCo == 1));  // EMCo = 1 has been added as Kent only wants to see this company
                return results;
            }
            else
            {
                List<EMEM> results = db.Select<EMEM>(p => p.Where(ev => ev.EMCo == 1)); // EMCo = 1 has been added as Kent only wants to see this company
                return results;
            }
        }
    }

但我的问题是,正如您所看到的,它返回的 EMEM 不包含 extraField1 和 extraField2

But my problem is that as you can see it's returning EMEM which doesn't contain extraField1 and extraField2

当我调试返回结果"时,值或 extraField1 和 extraFeild2 在结果中,但由于它们不在 EMEM 中,因此不会在响应中发送.

When I debug the "return result" the values or extraField1 and extraFeild2 are in the results but as they are not in EMEM they are not being sent in the response.

如何将结果放入 EMEMResponse 并返回该结果而不是 EMEM?

How can I get the result into EMEMResponse and return that instead of EMEM?

是否与自动映射有关?我似乎无法解决.

Is it something to do with AutoMapping? I just cannot seem to work it out.

任何帮助将不胜感激.

谢谢

编辑

我尝试了以下操作,但没有一个值被复制...

I have tried the following but none of the values get copied across...

var test1 = new EMEMResponse { }.PopulateWith(results);
var test2 = results.ConvertTo<EMEMResponse>();

推荐答案

这是一个控制台应用程序,用于从一个类型集合转换为另一个类型集合(在 ServiceStack v4.0.9 上测试)

Here is a console application to convert from one typed collection to another (tested on ServiceStack v4.0.9)

using System;
using System.Collections.Generic;
using ServiceStack;


public class Employee
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class EmployeeModel
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Program
{
    public static void Main()
    {
        var results = new List<Employee> {
            new Employee { Id = "BG", FirstName = "Bill", LastName = "Gates" },
            new Employee { Id = "SJ", FirstName = "Steve", LastName = "Jobs" }
        };

        var results2 = results.ConvertAll(x => x.ConvertTo<EmployeeModel>());

        foreach (var result in results2)
        {
            // Will display EmployeeModel instance
            Console.WriteLine(string.Format("{0} {1} {2}", result.Id, result.FirstName, result.LastName));
        }
        Console.Read();
    }
}

这篇关于Servicestack Ormlite 自动映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆