9

I've created a simple parameterless stored procedure which I've pasted below. I've imported the stored procedure to my Entity Model and created a Function Import. The function in the model is never created and I am unable to execute this stored procedure using the ADO.NET Entity Framework. I've opened the .edmx file in XML view and have confirmed there are no errors regarding this stored procedure. What am I doing wrong? Is it really impossible to call such a simple stored procedure from the Entity Framework? I've set the return type for the import function to None seeing this stored procedure does not need to return any recordsets or values.

Stored Procedure:

ALTER PROC [dbo].[Inventory_Snapshot_Create]

AS

SET NOCOUNT ON

DECLARE @Inventory_Snapshot_ID int

INSERT INTO Inventory_Snapshots (snapshot_timestamp)
VALUES (GETDATE())

SET @Inventory_Snapshot_ID = SCOPE_IDENTITY()

INSERT INTO Inventory_Snapshot_Products (inventory_snapshot_id,
    idProduct, stock)

    SELECT @Inventory_Snapshot_ID, idProduct, stock
    FROM products


SET NOCOUNT OFF

Code trying to execute stored procedure:

Dim db As New MilkModel

db.Inventory_Snapshot_Create()

7 Answers 7

11

First you add it to the model, then you go to the Entity Container, then the Import the function.

Full details here:

http://msdn.microsoft.com/en-us/library/bb896231.aspx

1
  • This is totally correct. No need for nasty hacks here - you just need to read the docs.
    – Stuart
    Feb 1, 2011 at 20:13
6

Thanks, pmarflee.

I actually came here to post my resolution to this and saw your response at the same time. This code actually uses the entity framework's connection and executes the stored procedure I imported into the model. Microsoft keeps pushing us developers to use Entity Framework instead of LINQ to SQL and other DAL generators but EF really isn't where it needs to be at all. I won't be using it in future projects until it's a more complete solution.

Here's what I ended up doing:

Dim db As New MilkModel

'==
'Begin dirty hack to execute parameterless/resultless stored
'procedure using Entity Framework (well, sort of using EF). 
'http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/44a0a7c2-7c1b-43bc-98e0-4d072b94b2ab/
'==
Dim con As DbConnection = db.Connection

con.Open()

Dim cmd As DbCommand = con.CreateCommand()

With cmd
    .CommandType = CommandType.StoredProcedure
    .CommandText = "MilkModel.Inventory_Snapshot_Create"
    .ExecuteNonQuery()
    .Dispose()
End With

con.Dispose()
'==
'End dirty hack
'==
2
  • Thanks for the work around, I too can't believe that you can't call stored procedures though this. but as stated by pmarflee, the reasoning is that if you are modifying the tables in your EF then you are not suppose to use it.
    – greektreat
    Mar 7, 2010 at 1:23
  • 2
    No need for a hack see this: msdn.microsoft.com/en-us/library/bb896231.aspx
    – Daniel O
    Feb 1, 2011 at 22:54
4

Any stored procedure can be called through EF. Create a SP and add it to your model. Further go to the model browser, right click the sp, and click add function import, here select your SP, and select what type it returns, if it returns nothing then select 'None'. If it returns a sclar type then select the type, and if its returning a recordset then select complex. Click on get column information, it will show you the colums returning. click on create new complex type. and click ok. If its returning one of you entities then click on option entity and select the entity type from the list box.

now you call call it in your code.

if its returning a complex type use code:

MyEntities _entities = new MyEntities(); var p = from d in _entities.GetOrderInfo() select d;

if its returning nothing then use code: MyEntities _entities = new MyEntities(); _entities.Cancel_Sale(ucode, oid);

for further queries visit

http://dubeyniraj.blogspot.com/

1
  • If you are using EF5 onwards there is no function import, you can access them directly without function import.
    – Niraj
    May 10, 2014 at 8:29
3

I don't think you can add a stored procedure to an EF model unless it is associated with a particular CRUD operation on an entity. However you can reference the Connection property of your entity container object to access the underlying ADO.NET connection object that the EF is using. You can then call your stored procedure using traditional ADO.NET code.

0
1

In case anyone else wonders about this...

The main problem with stored procs and the Entity Framework is that the return types need to be definted. For example, if you have a stored proc that returns soem rows from a table, the Entity Framework needs to be told what the structure of those rows looks like. It needs this so that it can generate all the proxy classes and serialize and deserialize things.

When you go to the model - rigkt click - inport function, you can choose a stored proc, but it expects the return types to be none, scalar, or entity types (e.e. based off of the tables in your database). You can, if you wish, add a complex type. Right click model - choose add - Complex type. You then define the columns and their data types.

Once this is done, the class you just created will show up under the list of entity types mentioned above.

1

For my project I'm actually using a FirebirdSql DB, I dunno if it's really right for Entity Framework, since I used it give me some issues and it's not so performant.

But anyway if I (function) import a stored procedure with some insert operations within, for some reason nothing happens. In other words if I add to my model (EDCX) such SP:

create procedure insert_stuff(thing varchar(40))
returns response(50)
as
begin
    insert into stuffs(thing) values (thing);

    response = 'done!';

    suspend;
end;

and then I call it in this way:

FirebirdContext.InsertStuff("Hi there!"); // it should return an ObjectResult<String>
FirebirdContext.SaveChanges();

well nothing actually happens. Nothing at all. Neither exceptions nor results nor inserts, everthing is perfect, no warnings no compile errors. The funny thing is that if I put a delete into the stored procedure script, the delete works!! But no inserts!

The table is very simple as shown and I already imported other stored procedures and they work, but because they contain queries, updates and delete, in the case they got insert... no way! Could it be a bug in the Firebird Ado.NET Provider? Or do I have to set something I've missed?

0

if want to avoid using a DBCommand, I made it work by making the SP return some set of columns that match with an entity definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.