带有存储过程的 Viemodel
我正在关注 scott gu 的教程 此处使用存储过程检索数据。我通过将 SProc 放在类上来设置存储过程的返回类型。现在我的问题是,我们可以设置一些 viewmodel
作为存储过程的返回类型,因为我的视图是使用我的 viewmodel
强类型的
I am following the tutorial of scott gu from Here to retrieve the data using stored procedure. i am setting the return type of the stored procedure by dropping the SProc on class. Now my question is that can we set some viewmodel
as the return type of the stored procedure, as my view is strongly typed with my viewmodel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将存储过程的返回类型与某些自定义类型相关联:在.edmx设计中,单击导入的存储过程并将函数导入名称设置为SomeCustomName,然后选择返回类型->实体->某个实体。现在,存储过程返回实体模型,您可以将这些模型映射到最终返回到视图的视图模型。最好的做法是使用针对视图定制的视图模型,而不是直接传递 SQL 传输对象。
You can associate the return type of the stored procedure to some custom type: In .edmx design click on the imported stored procedure and set the function import name to SomeCustomName and select the return Type -> Entities -> SomeEntity. Now having the stored procedure return entity models you could map those models to a view model that is eventually returned to the view. It is considered good practice to have view models tailored to the views instead of passing directly the SQL transport objects.
我写了一个数据库视图而不是viewmodel,然后将视图拖到dbml中并设置存储过程的返回类型来查看。这解决了我的问题。这可能对其他人有帮助
I have written a database view instead of viewmodel and then drag the view to dbml and set the return type of stored procedure to view. That solve my problem. It may be helpful for some one else
[我还没有对 ASP.NET MVC 做任何事情,所以这个建议可能还很遥远,但是...]
一种选择是通过右键单击 -> 添加 -> 在 dbml 设计器中创建 ViewModel 类;班级。然后,将存储过程放到设计器中的新类上,或者如果失败(就像我经常做的那样),将存储过程放到设计器表面上并设置其返回类型(属性 -> 返回类型)到新班级。
这是让存储过程返回自定义类型的一种方法,该类型不是真正的 LINQ to SQL 实体,也不是自动生成的存储过程 *Result 类。 (因为我听说将模型对象传递给视图是一种不好的形式 - 理想情况下你传递一个 ViewModel 对象,对吧?)。
[I have yet to do anything with ASP.NET MVC, so this suggestion may be way off, but...]
One option would be to create your ViewModel class in your dbml designer by going right-click->Add->Class. Then, either drop your stored procedure on the new class in the designer, or if that fails (like it often does, for me), drop the stored procedure onto the designer surface and set its return type (Properties -> Return Type) to the new class.
That's one way to have a stored procedure return a custom type that's not a true LINQ to SQL entity and not an autogenerated stored procedure *Result class. (Since I've heard it's bad form to pass model objects to the view - ideally you pass a ViewModel object, right?).