Manually added stored procedures in edmx file

Is it possible to constantly add stored procedures manually added to the edmx file? The database is generated from the model. Every time I change something in the editor in the edmx file, stored procedures are lost. After that, only the FunctionImport entry will be available.

Function example is as follows:

 <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> 

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> 
+5
source share
2 answers

Moo, you should not manually modify the edmx file.

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

That is why you are losing your job. You must map the already created stored procedure from the designer, just as you would with your tables.

I assume you are playing with EF4.

+2
source

This solution works if you used Model First and do not want to generate a model from the database. (using VS2013, EF6, MVC5)

Open the EDMX file. Right-click and click Update Model from Database. Check only the "Stored Procedures and Functions" and uncheck the two fields at the bottom, except for "Import selected stored procedures and functions into the entity model" (see Figure below)

Update model wizard

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

Link: Using stored procedures in the first approach of the Entity Framework model (Part I - Adding a new stored procedure)


Further explanation and details:

"When the Entity data model wizard creates a .edmx file from the database, it creates entries in the warehouse model for each stored procedure in the database. The corresponding entries are added to the conceptual model when creating the import function." How to import a stored procedure (entity data model tools)

It seems you need to update your model from the database in order to use the "Add New> Import Function .." wizard to import stored procedures. Otherwise, it will not appear in the list. If you add them manually, you run the risk of overwriting them. This will happen every time you edit your model. You will need to update this code at any time when you make changes to the model or database (I would save a copy of it).

Here's how you do it when creating a model from a database: EF model first with stored procedures

They also refer to updating the model from the database on this MSDN community support forum: how to add a new stored procedure to an existing .edmx file

This is the only way I found updating stored procedures WITHOUT updating the model.

+1
source

Source: https://habr.com/ru/post/1387936/


All Articles