将复杂的结果集从存储过程转换为实体及其子节点

时间:2014-05-10 17:12:25

标签: entity-framework entity-framework-6

我有一个存储过程,它返回一系列连接表结果作为几个结果集之一,ala:

SELECT 
    a.*,
    at.*
FROM
    dbo.UserProfile up
JOIN
    dbo.Players p ON up.UserId = p.UserId
JOIN
    dbo.PlayerAbilities pa ON p.PlayerId = pa.PlayerId
JOIN
    dbo.Abilities a ON pa.AbilityId = a.AbilityId
JOIN
    dbo.Attributes at ON a.AttributeId = at.AttributeId
WHERE
    up.UserName = @userName

我的Ability类的实体类结构如下:

class Ability
{
    /* Standard Properties */

    public Attribute Attribute { get; set; }
}

当我使用代码调用此存储过程时:

//Establish that var reader = (stored procedure).ExecuteReader()
var abilities = ((IObjectContextAdapter)context)
                        .ObjectContext
                        .Translate<Ability>(reader);

我获得了Ability对象的集合,但是没有填充它们的Attribute属性。如何修改我的存储过程或指示实体框架以允许填充此属性?

编辑:请求的属性和能力表模式:

TABLE Attributes
{
    AttributeId INT PKEY
    AttributeName VARCHAR(50)
}
TABLE Abilities
{
    AbilityId INT PKEY
    AbilityName VARCHAR(50)
    AbilityDescription VARCHAR(500)
    AttributeId INT FKEY => Attributes.AttributeId
    ClassificationId INT FKEY => Classifications.ClassificationId
    BaseDamage INT
    LevelRequired INT
    PriceId INT FKEY => Prices.PriceId
    RecordStatusId INT FKEY => RecordStatuses.RecordStatusId
}

忘记在原始编辑中添加:

class Attribute
{
    public int AttributeId { get; set; }
    public string AttributeName { get; set; }
    public ICollection<Ability> Abilities { get; set; }

    //Never used, doesn't matter for lazy loading though, never got around to deleting
    public virtual ICollection<Monster> Monsters { get; set; }
    public virtual ICollection<Monster> Monsters1 { get; set; }
}

我最初在上述属性问题中没有包含分类/价格/记录状态列信息,因为我虽然它是多余的,但我已将它们包含在此处,以防它们相关。< / p>

1 个答案:

答案 0 :(得分:0)

翻译不知道如何处理导航属性,但是如果单独查询和EF中的查询属性应该修复关系。