Stored procedure returns multiple entity sets

Source: Internet
Author: User

The default value of the enumeration, which is the value of the underlying type for the enumeration value of 0 (the default value is 0 if there is no corresponding enumeration value)

When you install the Entity Framework using NuGet, Migrate.exe is located in the Tools folder of the download package, in < Project folders >\packages\EntityFramework.< version >\tools
After you have migrate.exe, you need to copy it to the location of the assembly that contains the migration.

Modefist The table structure is generated, in the EF designer, right-click on the empty location on the model and select "Add code Generation Item ..." To generate the DbContext context

Using the EF call stored procedure to put back multiple result sets

Create a stored procedure

CREATE PROCEDURE [dbo]. [Getallblogsandposts]
As
SELECT * FROM dbo. Blogs
SELECT * FROM dbo. Posts

By default, this stored procedure returns only the type result of bogs

Solution:

Way One

1 using(vardb =NewBloggingcontext ())2 { 3     //If using Code First we need to make sure the model is built before we open the connection4     //This isn ' t required for models created with the EF Designer5Db. Database.initialize (Force:false); 6  7     //Create a SQL command to execute the sproc8     varcmd =db. Database.Connection.CreateCommand (); 9Cmd.commandtext ="[dbo]. [Getallblogsandposts]"; Ten   One     Try  A     {  -           - db. Database.Connection.Open ();  the         //Run The sproc -         varReader =cmd. ExecuteReader ();  -   -         //Read Blogs from the first result set +         varBlogs =((iobjectcontextadapter) db) -             . ObjectContext +. Translate<blog> (Reader,"Blogs", mergeoption.appendonly);  A   at   -         foreach(varIteminchblogs) -         {  - Console.WriteLine (item. Name);  -         }          -   in         //Move to second result set and read Posts - Reader. NextResult ();  to         varPosts =((iobjectcontextadapter) db) +             . ObjectContext -. Translate<post> (Reader,"Posts", mergeoption.appendonly);  the   *   $         foreach(varIteminchposts)Panax Notoginseng         {  - Console.WriteLine (item. Title);  the         }  +     }  A     finally  the     {  + db. Database.Connection.Close ();  -     }  $}
View Code

Way Two

Modifying the edmx file file to modify the complex type returned by the stored procedure to the specified entity type (this method is overwritten after the model is updated)

1. Modify the XML corresponding to the EDMX

1<!--CSDL Content--2<edmx:ConceptualModels>3  4 ... 5  6<functionimport name="getallblogsandposts"Returntype="Collection (Blogmodel.getallblogsandposts_result)"/>7  8 ... 9  Ten<complextype name="Getallblogsandposts_result"> One<property type="Int32"Name="BlogId"nullable="false"/> A<property type="String"Name="Name"nullable="false"Maxlength="255"/> -<property type="String"Name="Description"nullable="true"/> -</ComplexType> the   - ...  -   -</edmx:ConceptualModels> +  - modified to: +<functionimport name="getallblogsandposts"> A<returntype entityset="Blogs"Type="Collection (Blogmodel.blog)"/> at<returntype entityset="Posts"Type="Collection (blogmodel.post)"/> -</FunctionImport> -  -  -<!--c-s mapping Content-- -<edmx:Mappings> in   - ...  to   +<functionimportmapping functionimportname="getallblogsandposts"Functionname="BlogModel.Store.GetAllBlogsAndPosts"> -<ResultMapping> the<complextypemapping typename="Blogmodel.getallblogsandposts_result"> *<scalarproperty name="BlogId"Columnname="BlogId"/> $<scalarproperty name="Name"Columnname="Name"/>Panax Notoginseng<scalarproperty name="Description"Columnname="Description"/> -</ComplexTypeMapping> the</ResultMapping> +</FunctionImportMapping> A           the ...  +   -</edmx:Mappings> $  $ modified to: -<ResultMapping> -<entitytypemapping TypeName ="Blogmodel.blog"> the<scalarproperty name="BlogId"Columnname="BlogId"/> -<scalarproperty name="Name"Columnname="Name"/>Wuyi<scalarproperty name="Description"Columnname="Description"/> the</EntityTypeMapping> -</ResultMapping> Wu<ResultMapping> -<entitytypemapping typename="Blogmodel.post"> About<scalarproperty name="BlogId"Columnname="BlogId"/> $<scalarproperty name="PostID"Columnname="PostID"/> -<scalarproperty name="Title"Columnname="Title"/> -<scalarproperty name="Text"Columnname="Text"/> -</EntityTypeMapping> A</ResultMapping>
View Code

2. Execute the following code

1 using(vardb =Newblogentities ())2 { 3     varResults =db. Getallblogsandposts (); 4  5     foreach(varResultinchresults)6     { 7Console.WriteLine ("Blog:"+result. Name); 8     } 9  Ten     varPosts = results. Getnextresult<post>();  One   A     foreach(varResultinchposts) -     {  -Console.WriteLine ("Post:"+result. Title);  the     }  -   - console.readline (); -}
View Code

Note : The order in which the Entity data is read in these two ways must match the order of the entities returned in the stored procedure, and no one will error

Stored procedure returns multiple entity sets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.