2

I'm using EF 5.

I have a working stored procedure which takes a dataset and inserts all the data into a table (its a simple merge type thing).

I've mapped the storedprocedure to my insert through the modelbrowsers "Stored procedure mapping" hoping it would "magically" work.

I thought this would actually insert the dataset when doing

db.mytable.AddRange(List<Entity>)

The problem is, that the Stored Procedure gets called, but with individual rows and not a Dataset.

How do I insert a Dataset through a storedprocedure using EF?

I've tried googling for it, but all i could come up with is how to return a dataset from an EF sp - but that's not what i'm looking for.

Thank you in advance.

Update

I've followed the link Richard provided and found a "solution"

Create extension method

 public static void ExecuteStoreProcedure(this ObjectContext context, string storeProcName, params object[] parameters)
        {
            string command = "EXEC " + storeProcName + " @Table";

            context.ExecuteStoreCommand(command, parameters);
        }

where @Table is the parameter name that the SP takes for my dataset/datatable

Then doing like so:

 using (var db = new MyEntities())
            {
                var Table = new SqlParameter("Table", SqlDbType.Structured);
                Table.Value = agentList.ToDataTable();
                Table.TypeName = "dbo.MyDataSetType";

                var objectContext = ((IObjectContextAdapter)db).ObjectContext;
                objectContext.ExecuteStoreProcedure("dbo.p_storedproc",Table);                         
            }

where Table again is the parameter name and the TypeName is the Dataset/Datatable type-name used for the parameter in the stored procedure.

I'm not quite happy with this solution - It works, but I would like a more functional way of doing it (more "EF like").

VisualBean
  • 4,908
  • 2
  • 28
  • 57
  • 1
    http://stackoverflow.com/questions/8157345/entity-framework-stored-procedure-table-value-parameter Looks like it might be relevant – Richard Apr 21 '15 at 07:40
  • Followed your link and came up with a solution - I'm not happy with it, but it works :) – VisualBean Apr 21 '15 at 08:08

0 Answers0