Manually added stored procedures in edmx file

后端 未结 2 1563
予麋鹿
予麋鹿 2021-01-13 02:57

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 edit

相关标签:
2条回答
  • 2021-01-13 03:27

    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.

    0 讨论(0)
  • 2021-01-13 03:30

    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.

    0 讨论(0)
提交回复
热议问题