niklr
niklr

Reputation: 1701

Manually added stored procedures in edmx file

Is it possible to persistently add the manually added stored procedures in the edmx file? The database is generated from model. Every time I change something within the editor in the edmx file, the stored procedures are lost. Only the FunctionImport entry is still available afterwards.

A sample Function looks like this:

<Function Name="SP_I_InsertGroup" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
    <Parameter Name="name" Type="nvarchar" Mode="In" />
    <Parameter Name="chiefId" Type="int" Mode="In" />
    <Parameter Name="description" Type="nvarchar" Mode="In" />
    <Parameter Name="parentId" Type="int" Mode="In" />
    <Parameter Name="mode" Type="char" Mode="In" />
</Function>

The corresponding FunctionImportMapping:

<FunctionImportMapping FunctionImportName="InsertGroup" FunctionName="DAL.Store.SP_I_InsertGroup" />

FunctionImport:

<FunctionImport Name="InsertGroup" ReturnType="Collection(Int32)">
    <Parameter Name="name" Mode="In" Type="String" />
    <Parameter Name="chiefId" Mode="In" Type="Int32" />
    <Parameter Name="description" Mode="In" Type="String" />
    <Parameter Name="parentId" Mode="In" Type="Int32" />
    <Parameter Name="mode" Mode="In" Type="String" />
</FunctionImport>

Upvotes: 5

Views: 9958

Answers (2)

Termato
Termato

Reputation: 1610

This solution works if you did Model First and don't want to generate the model from the database. (Using VS2013, EF6, MVC5)

Open your EDMX file. Right Click and press Update Model from Database. Only check "Stored Procedures and FUnctions" and uncheck the two boxes at the bottom except "Import Selected Stored Procedures and functions into the entity model" (see image below)

Update Model Wizard

Note: If you have data annotations, etc., they will be regenerated and overwritten.

Reference: Using stored procedures in Entity Framework Model First Approach (Part I - Adding New Stored procedure)


Further Explanation and Detail:

"When the Entity Data Model Wizard generates an .edmx file from a database, it creates entries in the storage model for each stored procedure in the database. Corresponding entries are added to the conceptual model when function imports are created." How to: Import a Stored Procedure (Entity Data Model Tools)

It appears you have to update your model from the database in order to use the "Add New > Function Import.." wizard to import a stored procedures. Otherwise, it wont show up on the list. If you add them manually, you do run the risk of them being overwritten. This will happen every time you edit your model. You'd have to keep that code up to date anytime you made changes to the model or database (I'd keep a back up copy of it).

Here is how you do it by generating the model from the database: EF Model First with Stored Procedures

They also reference updating the model from database in this MSDN Community Support Forum: how to add new stored procedure to existing .edmx file

This is the only way I've found to update the stored procedures WITHOUT updating the model.

Upvotes: 3

Fergara
Fergara

Reputation: 947

Moo, you should not modify the edmx file by hand.

"Changes to this file may cause incorrect behavior and will be lost if the code is regenerated."

That´s why you´re losing your work. You should map the already created stored procedure from the designer, the same way you made with your tables.

I´m assuming you´re playing with EF4.

Upvotes: 2

Related Questions