locked
How to return multiple return types in Function Import RRS feed

  • Question

  • User1081236289 posted

    Hi,

         I have a SP which return multiple tables. In Entity Framework 4, when i try to add multiple return set to FunctionImport, like below

    <FunctionImport Name="GetAllBlogsAndPosts"
      <ReturnType EntitySet="Blogs" Type="Collection(BlogModel.Blog)" /> 
      <ReturnType EntitySet="Posts" Type="Collection(BlogModel.Post)" /> 
    </FunctionImport>

    It says error like "ReturnType" tag cannot be added to FunctionImport. 

    Can anyone please tell me as how can i have multiple return types for a function in EF. 

    Thanks

    Thursday, June 25, 2015 3:07 AM

Answers

  • User-271186128 posted

    Hi Mayil_Gilli,

    With reference to this article, I found that you must target .NET Framework 4.5 to be able to configure multiple result sets in EDMX. If you are targeting .NET 4.0 you can use the code-based method shown in the previous section.

    So, since you are using Enitity Framework 4, I suggest you could use the following code to access Multiple Result Sets.

    using (var db = new BloggingContext()) 
    { 
        // If using Code First we need to make sure the model is built before we open the connection 
        // This isn't required for models created with the EF Designer 
        db.Database.Initialize(force: false); 
     
        // Create a SQL command to execute the sproc 
        var cmd = db.Database.Connection.CreateCommand(); 
        cmd.CommandText = "[dbo].[GetAllBlogsAndPosts]"; 
     
        try 
        { 
             
            db.Database.Connection.Open(); 
            // Run the sproc  
            var reader = cmd.ExecuteReader(); 
     
            // Read Blogs from the first result set 
            var blogs = ((IObjectContextAdapter)db) 
                .ObjectContext 
                .Translate<Blog>(reader, "Blogs", MergeOption.AppendOnly);    
     
     
            foreach (var item in blogs) 
            { 
                Console.WriteLine(item.Name); 
            }         
     
            // Move to second result set and read Posts 
            reader.NextResult(); 
            var posts = ((IObjectContextAdapter)db) 
                .ObjectContext 
                .Translate<Post>(reader, "Posts", MergeOption.AppendOnly); 
     
     
            foreach (var item in posts) 
            { 
                Console.WriteLine(item.Title); 
            } 
        } 
        finally 
        { 
            db.Database.Connection.Close(); 
        } 
    }

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 25, 2015 11:28 PM