我试图从实体框架

时间:2015-11-08 12:39:41

标签: c# xml entity-framework-6

我使用Web API创建了一个MVC5 Web应用程序。 我还使用了Entity Framework 6。

我有一个存储过程:

SELECT * FROM [Group] WHERE CompanyRef = @CompanyRef OR CompanyRef = '00000000-0000-0000-0000-000000000000' AND Active = 1 
FOR XML PATH('Group'), ROOT ('Groups')

它在查询分析器中返回:

<Groups>
  <Group>
    <GroupId>1</GroupId>
    <GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
    <Name>Todays Work</Name>
    <Description>System</Description>
    <CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef>    
    <Active>1</Active>
  </Group>
  <Group>
    <GroupId>2</GroupId>
    <GroupRef>00000000-0000-0000-0000-000000000000</GroupRef> 
    <Name>All</Name>
    <Description>System</Description>
    <CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef> 
    <Active>1</Active>
  </Group>
</Groups>

Function Imports(EF)中声明的返回类型是一个字符串。

在我的服务课程中我连接&#39;通过调用它来实体模型:

public ObjectResult<string> Get(Guid? companyRef)
{
    return o.Group_Get(companyRef);
}

在我的API控制器中,我调用我的服务函数:

Compression compression = new Compression();
var test = groupRepository.Get(companyRef);

检查&#39; test&#39;的价值。我明白了:

enter image description here

没有我的XML的迹象吗?

1 个答案:

答案 0 :(得分:0)

您可以使用:

 using (SqlConnection con = new SqlConnection(_dbContext.Database.Connection.ConnectionString))
        {
            #region Connection Open

            SqlCommand cmd = new SqlCommand("mbl_GetCourseMat", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Par1", "par1");
            cmd.Parameters.AddWithValue("@Par2", 2);

            con.Open();

            #endregion

            #region Reader XML

            using (XmlReader reader = cmd.ExecuteXmlReader())
            {
                while (reader.Read())
                {
                    string result = reader.ReadOuterXml();
                    if (!String.IsNullOrEmpty(result))
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(MobilCourseMatModel));
                        using (MemoryStream ms = new MemoryStream())
                        {
                            byte[] buffer = Encoding.UTF8.GetBytes(result);
                            ms.Write(buffer, 0, buffer.Length);
                            ms.Position = 0;
                            using (XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8))
                            {
                                model = (MobilCourseMatModel)xs.Deserialize(ms);
                            }
                        }
                    }
                }
            }

            #endregion

            con.Close();
        }