显示名称型号上使用实体框架 [英] DisplayName on Model that use Entity Framework

查看:117
本文介绍了显示名称型号上使用实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code,它是这样的:

I have a code that look like this :

public class MyModel
{
    [Required]
    [Display(Name = "labelForName", ResourceType = typeof(Resources.Resources))]
    public string name{ get; set; }
}

问题是属性显示和要求已经在生成的模型类实体框架中被添加。我知道我可以用部分新增功能,但如何添加属性将被抹去,并与ORM更新的一类?

The problem is the attribute Display and Required have been added in the generated model class of Entity Framework. I know I can add functionality with Partial but how can I add attribute to a class that will be erase and updated with the ORM?

推荐答案

在我的经验,从数据库模型是很少一样在网页中被使用。你总是需要某种变化。因此,视图模型 S的用法。另一个好处是,如果实体模型更改使用您的ViewModel所有网页不会打破。

In my experience, models from a database are seldom the same as being used in web pages. You always need some kind of change. Hence the usage of ViewModels. Another upside is that all web pages that use your ViewModel won't break if the entity model is changed.

至于安全性,如果你有一个公众的ActionResult保存(MyEntityModel模型)可能导致安全漏洞,因为用户可以找出如何张贴值属性不应该改变(如角色状态 IsAdmin 或其他)。

As for security, if you have a public ActionResult Save(MyEntityModel model) can lead to a security breach since the user may figure out how to post values to properties that shouldn't be changed (like Role, Status, IsAdmin or whatever).

让自己熟悉映射器(如 automapper )来代替,并把属性的视图模型。

Get yourself familiar with a mapper (like automapper) instead, and put the attributes on the ViewModel.

这篇关于显示名称型号上使用实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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