ASP.NET MVC:使用EF实体的ViewModels? [英] ASP.NET MVC: using EF entities as viewmodels?

查看:498
本文介绍了ASP.NET MVC:使用EF实体的ViewModels?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1498927/asp-net-mvc-linq-to-entities-model-as-the-viewmodel-is-this-good-practice\">ASP.NET MVC - LINQ到实体模型视图模型 - 这是很好的做法

时即可使用EF的实体类作为视图模型在ASP.NET MVC?

Is is OK to use EF entities classes as view models in ASP.NET MVC?

如果视图模型是90%相同的EF实体类的?

What if viewmodel is 90% the same of EF entity class?

让我们说我有实体框架模型中的调查类。对于视图所需它90%的比赛数据进行编辑。
从应该具有什么视图模型的唯一差别 - 是在它被使用的一个或多个属性(所需要的填充调查对象,因为EF类不能被直接映射到它是如何属性重新presented(子复选框无线电基等))

Let's say I have a Survey class in Entity Framework model. It 90% matches data required for view to edit it. The only difference from what view model should have - is one or several properties to be used in it (that are required to populate Survey object because EF class cannot be directly mapped onto how it's properties are represented (sub-checkboxes, radio groups, etc.))

你使用计算机[]通过他们?或创建调查类(SurveyViewModel)与新的附加属性的副本(它应该能够将数据从调查和复制回它)?

Do you pass them using ViewData[]? Or create a copy of Survey class (SurveyViewModel) with new additional properties (it should be able to copy data from Survey and back to it)?

编辑:
我也想避免使用调查作为SurveyViewModel财产。它看起来奇怪,当一些调查属性是使用的UpdateModel或默认联更新,而其他(即不能直接映射到实体) - 在控制器使用SurveViewModel自定义属性

I'm also trying to avoid using Survey as SurveyViewModel property. It will look strange when some Survey properties are updated using UpdateModel or with default binder, while others (that cannot be directly mapped to entity) - using SurveViewModel custom properties in controller.

推荐答案

我喜欢使用<一个href=\"http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view-models.aspx\">Jimmy博加德的做法总是有一个1:视图和模型视图1的关系。换句话说,我不会用我的域模型(在这种情况下,您的EF实体)的视图模型。如果你觉得你做了很多工作映射两者之间,你可以使用类似 AutoMapper 做的工作给你的。

I like using Jimmy Bogard's approach of always having a 1:1 relationship between a view and a view model. In other words, I would not use my domain models (in this case your EF entities) as view models. If you feel like you are doing a lot of work mapping between the two, you could use something like AutoMapper to do the work for you.

这篇关于ASP.NET MVC:使用EF实体的ViewModels?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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