Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用实体框架的存储过程中同一模型中的多个复杂类型实例_C#_Entity Framework_Stored Procedures_Entity Framework 6 - Fatal编程技术网

C# 使用实体框架的存储过程中同一模型中的多个复杂类型实例

C# 使用实体框架的存储过程中同一模型中的多个复杂类型实例,c#,entity-framework,stored-procedures,entity-framework-6,C#,Entity Framework,Stored Procedures,Entity Framework 6,我首先使用实体框架代码,我有以下场景,我有一个成员模型,有两种类型的地址,居住地址和通信地址。Address是一个复杂类型,所以我在同一个模型中有两个相同复杂类型的实例。我从这里读到: ,但不涉及存储过程。 我的案例基本相同,但使用存储过程 以下是我的模型和存储过程中使用的查询 地址复合文本类型: public class Address { public string AddressLine1 { get; set; } public string AddressLine2 {

我首先使用实体框架代码,我有以下场景,我有一个成员模型,有两种类型的地址,居住地址和通信地址。Address是一个复杂类型,所以我在同一个模型中有两个相同复杂类型的实例。我从这里读到: ,但不涉及存储过程。 我的案例基本相同,但使用存储过程

以下是我的模型和存储过程中使用的查询

地址复合文本类型:

public class Address
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public State State { get; set; }
    public string Zip { get; set; }
    public Country Country { get; set; }
    public AddressType Type { get; set; }
}
成员类别:

public class Member
{
    public Address ResidenceAddress { get; set; }
    public Address CorrespondenceAddress { get; set; }
}
调用存储过程的方法:

public Member GetMember(string memberId)
{
    using (var entityContext = new AmigosContext())
    {
        var sqlParams = new[]
        {
            new SqlParameter("memberId", memberId), 
        };
        var result = entityContext.Database.SqlQuery<MemberInfoEnhanced>("EXEC GetMember @MemberId ", sqlParams);

        return result.FirstOrDefault(); 
    }
}
有没有办法将不同的地址列映射到不同的复杂类型


谢谢

不确定这是否与此有关,但在EF代码中,您不需要首先将外部属性设置为
虚拟
?ie
公共虚拟地址地址地址{get;set;}
SELECT  gcar.AddressLine1 AS ResidentAddressLine1
     , gcar.AddressLine2 AS ResidentAddressLine2
     , gcityr.Name AS ResidentCity
     , gcar.State AS ResidentState
     , gcar.Zip AS ResidentZip
     , gcountry.ISOAlpha AS ResidentCountryISOCode
     , gcar.AddressTypeId AS ResidentAddressTypeId
     , gcac.AddressLine1 AS CorrespondenceAddressLine1
     , gcac.AddressLine2 AS CorrespondenceAddressLine2
     , gcityc.Name AS CorrespondenceCity
     , gcac.State AS CorrespondenceState
     , gcac.Zip AS CorrespondenceZip
     , gcac.AddressTypeId AS CorrespondenceAddressTypeId

FROM Member pm WITH (NOLOCK)ON pm.MemberId = pme.MemberId
       LEFT JOIN Generic_ContactAddress gcar ON gcar.ContactBaseId = pm.ContactBaseId -- get resident address
                                                    AND gcar.AddressTypeId = 2 --Physical Address
       LEFT JOIN Generic_Country gcountry ON gcountry.CountryId = gcar.CountryId -- to get the ISOAlpha code
       LEFT JOIN Generic_City gcityr ON gcityr.CityId = gcar.CityId --to get the resident CityName
       LEFT JOIN Generic_ContactAddress gcac ON gcac.ContactBaseId = pm.ContactBaseId -- to get correspondence address
                                                    AND gcac.AddressTypeId = 1 --Correspondence (Mailing) Address
       LEFT JOIN Generic_City gcityc ON gcityc.CityId = gcar.CityId --to get the correspondence CityName
     
WHERE pm.MemberId = @MemberId