EF5 db.Database.SqlQuery映射返回的对象

时间:2022-07-17 16:39:22

I have two C# classes

我有两个C#类

public class SearchResult
{
    public int? EntityId { get; set; }
    public string Name { get; set; }
    public Address RegisteredAddress { get; set; }
}

and

public class Address
{
    public int? AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
}

this is used in a dbContext call to map out the returning objects from a database via EF5

这在dbContext调用中用于通过EF5从数据库中映射返回的对象

using (DbEntities db = new DbEntities())
{
    querySearchResult = db.Database.SqlQuery<SearchResult>(
        @"SELECT e.entity_id AS EntityId, e.entity_reg_name AS Name,
              a.address_1 AS [RegisteredAddress.Address1]
          FROM
              entity AS e
              LEFT JOIN address AS a ON e.entity_reg_addr_id = a.address_id",
        objectParameterList.ToArray()).ToList();
}

The problem I'm having is that I cant seem to get the address object mapped even though there is address data returned. The other properties of the searchResult map fine.

我遇到的问题是,即使返回了地址数据,我也似乎无法映射地址对象。 searchResult映射的其他属性很好。

1 个解决方案

#1


10  

SqlQuery doesn't support Complex Type

SqlQuery不支持复杂类型

What you should do is:

你应该做的是:

internal class TempResult
{
    public int? EntityId { get; set; }
    public string Name { get; set; }
    public int? AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
}

var tempResults = db.Database.SqlQuery<TempResult>(
    @"SELECT e.entity_id AS EntityId, e.entity_reg_name AS Name,
          a.address_1 AS [RegisteredAddress.Address1]
      FROM
          entity AS e
          LEFT JOIN address AS a ON e.entity_reg_addr_id = a.address_id",
    objectParameterList.ToArray()).ToList();

querySearchResult = tempResults.Select(t => new SearchResult
{
    EntityId = t.EntityId,
    [...]
    RegisteredAddress = new Address 
        {
            AddressId = t.AddressId,
            [...]
        }
}

#1


10  

SqlQuery doesn't support Complex Type

SqlQuery不支持复杂类型

What you should do is:

你应该做的是:

internal class TempResult
{
    public int? EntityId { get; set; }
    public string Name { get; set; }
    public int? AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
}

var tempResults = db.Database.SqlQuery<TempResult>(
    @"SELECT e.entity_id AS EntityId, e.entity_reg_name AS Name,
          a.address_1 AS [RegisteredAddress.Address1]
      FROM
          entity AS e
          LEFT JOIN address AS a ON e.entity_reg_addr_id = a.address_id",
    objectParameterList.ToArray()).ToList();

querySearchResult = tempResults.Select(t => new SearchResult
{
    EntityId = t.EntityId,
    [...]
    RegisteredAddress = new Address 
        {
            AddressId = t.AddressId,
            [...]
        }
}