操作返回类型的Entity Framework Model First

时间:2013-07-11 13:32:04

标签: c# entity-framework

您好我正在使用Entity Framework数据库第一个模型。我使用自己的存储过程来访问数据,我似乎从实体框架中得到了一些类型的结果。

这是我的存储过程:

ALTER PROCEDURE [eConnect].[GetUserPersonalDetails]
@personId int
AS
BEGIN
    SELECT Persons.FirstName            as FirstName,
           Persons.MiddleName           as MiddleName, 
           Persons.LastName             as LastName,
           Persons.DateOfBirth          as DateOfBirth,
           Persons.CityOfBirth          as CityOfBirth,
           Persons.BSN                  as BSN,
           Persons.SecondaryEmail       as SecondaryEmail,
           Persons.HomePhone            as HomePhone,
           Persons.WorkPhone            as WorkPhone,
           Persons.MobilePhone          as MobilePhone,
           MaterialAddress.StreetName   as MaterialAddressStreetName,
           MaterialAddress.PostCode     as MaterialAddressPostCode,
           MaterialAddress.City         as MaterialAddressCity,
           MaterialAddressCountry.Name  as MaterialAddressCountry,
           HomeAddress.StreetName       as HomeAddressStreetName,
           HomeAddress.PostCode         as HomeAddressPostCode,
           HomeAddress.City             as HomeAddressCity,
           HomeAddressCountry.Name      as HomeAddressCountry,
           PostalAddress.StreetName     as PostalAddressStreetName,
           PostalAddress.PostCode       as PostalAddressPostCode,
           PostalAddress.City           as PostalAddressCity,
           PostalAddressCountry.Name    as PostalAddressCountry,
           Company.Name                 as CompanyName,
           Company.Phone                as CompanyPhone,
           Avatar.FileName              as FileName,
           Avatar.RealName              as RealName
    FROM Sync_Persons as Persons
    LEFT JOIN Sync_Addresses as MaterialAddress ON Persons.MaterialAddressID = MaterialAddress.AddressID 
                                                AND MaterialAddress.Deletion_Date IS NULL   
    LEFT JOIN Countries as MaterialAddressCountry ON MaterialAddress.CountryID = MaterialAddressCountry.CountryID 
                                                  AND MaterialAddressCountry.Deletion_Date IS NULL  
    LEFT JOIN Sync_Addresses as HomeAddress ON Persons.HomeAddressID = HomeAddress.AddressID 
                                            AND HomeAddress.Deletion_Date IS NULL
    LEFT JOIN Countries as HomeAddressCountry ON HomeAddress.CountryID = HomeAddressCountry.CountryID 
                                              AND HomeAddressCountry.Deletion_Date IS NULL
    LEFT JOIN Sync_Addresses as PostalAddress ON Persons.PostalAddressId = PostalAddress.AddressID 
                                              AND PostalAddress.Deletion_Date IS NULL
    LEFT JOIN Countries as PostalAddressCountry ON PostalAddress.CountryID = PostalAddressCountry.CountryID 
                                                AND PostalAddressCountry.Deletion_Date IS NULL
    LEFT JOIN Sync_Companies as Company ON Persons.CompanyID = Company.CompanyID 
                                        AND Company.Deletion_Date IS NULL
    LEFT JOIN Documents as Avatar ON Persons.AvatarPictureDocumentID = Avatar.DocumentID 
                                  AND Avatar.Deletion_Date IS NULL
    WHERE Persons.PersonID = @personId

在添加更新模型并在代码中调用此过程之后:

 m_DataContext.GetUserPersonalDetails(personId);

我回来了:

ObjectResult<GetUserPersonalDetails_Result>

在我的情况下,这个存储过程将始终返回一个值,因为personId将是我的表标识符。

我希望得到一个GetUserPersonalDetails_Result类型的对象。

如何告诉Entity Framework返回GetUserPersonalDetails_Result或如何将ObjectResult转换为GetUserPersonalDetails_Result?

1 个答案:

答案 0 :(得分:2)

ObjectResult只是collection of results。您可以使用First()Single()或您在集合中通常使用的任何内容来获得所需的结果。它成为一个集合,因为实体框架永远不能保证您的商店程序始终返回一个结果。