EF 6 数据库优先:如何更新存储过程? [英] EF 6 database first: How to update stored procedures?

本文介绍了EF 6 数据库优先:如何更新存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Entity Framework 6.0.0 并首先使用数据库(像这样) 从表和存储过程生成代码.这似乎很有效,只是在更新或刷新模型时不会反映存储过程中的更改.向表中添加一列会被反映,但不会向存储过程中添加字段.

We are using Entity Framework 6.0.0 and use database first (like this) to generate code from tables and stored procedures. This seems to work great, except that changes in stored procedures are not reflected when updating or refreshing the model. Adding a column to a table is reflected, but not adding a field to a stored procedure.

有趣的是,如果我转到Model Browser,右键单击存储过程,选择Add Function Import,然后单击按钮Get Column Information 我们可以看到正确的列.这意味着模型知道列,但无法更新生成的代码.

It is interesting that if I go to the Model Browser, right click the stored procedure, select Add Function Import and click the button Get Column Information we can see the correct columns. This means that the model knows of the columns, but does not manage to update the generated code.

有一种解决方法,那就是在更新模型之前删除生成的存储过程.只要您没有对存储过程进行任何编辑,这就会起作用.有没有人知道避免这种解决方法的方法?

There is one workaround, and that is to delete the generated stored procedure before updating the model. This works as long as you have not made any edits on the stored procedure. Does anyone know of a way to avoid this workaround?

我使用的是 Visual Studio 2013 以及截至 2013 年 12 月上旬的所有最新更新.

I am using Visual Studio 2013 with all the latest updates as of early December 2013.

提前致谢!

更新 1:在一种情况下,andersr 的回答有帮助,其中存储过程使用了临时表,所以我给了他 +1,但它仍然没有解决更新简单存储过程的主要问题.

Update 1: andersr's answer helped in one case, where the stored procedure used a temporary table, so i gave him +1, but it still does not solve the main problem of updating simple stored procedures.

更新 2:下面 shimron 的评论链接到有关 EF 3.5 中相同问题的问题.EF 6.0 似乎仍然如此.阅读它以了解另一种方法,但我现在的结论是,最简单的方法是在更新模型之前删除生成的存储过程.如果您想做一些奇特的事情,请使用部分类.

Update 2: shimron's comment below links to a question about the same issues in EF 3.5. It seems the same is still true for EF 6.0. Read it for an alternative way of doing it, but my conclusion as of now is that the simplest way of doing it is to delete the generated stored procedure before updating the model. Use partial classes if you want to do something fancy.

推荐答案

基于 this answer by DaveD,这些步骤解决了这个问题:

Based on this answer by DaveD, these steps address the issue:

  1. 在您的 .edmx 中,按住 rt 键并选择模型浏览器.
  2. 在模型浏览器中(在 VS 2015 默认配置中,它是解决方案资源管理器中的一个选项卡),展开模型下的函数导入.
  3. 双击您的存储过程.
  4. 点击Returns a Collection Of - Complex(如果不返回标量或实体)旁边的更新按钮
  5. 单击确定,然后保存您的 .edmx 以反映整个项目中存储过程的字段更改.
  1. In your .edmx, rt-click and select Model Browser.
  2. Within the Model Browser (in VS 2015 default configuration, it is a tab within the Solution Explorer), expand Function Imports under the model.
  3. Double-click your stored procedure.
  4. Click the Update button next to Returns a Collection Of - Complex (if not returning a scalar or entity)
  5. Click okay then save your .edmx to reflect field changes to your stored procedure throughout your project.

这篇关于EF 6 数据库优先:如何更新存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆