如何在实体框架中使用AllowHtml属性 [英] How do i use the AllowHtml attribute with the entity framework

查看:135
本文介绍了如何在实体框架中使用AllowHtml属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何在实体框架生成的类中添加 [AllowHtml] 属性,而在下次生成代码时不覆盖该属性?

How would i go about adding the [AllowHtml] attribute to an entity framework generated class without the attribute being overwritten the next time the code is generated?

我希望只允许CKEditor使用Razor将信息发布回我的MVC4应用程序,而不必使用 [ValidateReuqest(false)] 在我的 Content 实体类中。

I am looking to simply allow CKEditor to post information back to my MVC4 application using Razor without having to use [ValidateReuqest(false)] on my Content entity class.

推荐答案

您可以使用 [MetadataType] 属性可将元数据/属性永久添加到您的类中,而无需编辑原始类。

You can use the [MetadataType] attribute to add metadata/attributes to your classes permanently without having to edit the original classes.

class Content 在您的项目中创建一个新的CS文件,并将空类替换为:

For the class Content create a new cs file in your project and replace the empty class with:

[MetadataType(typeof(ContentMetadata))]
public partial class Content
{

}

public class ContentMetadata
{
    [AllowHtml]
    public string ContentHtml { get; set; }
}

部分类名必须与实体类的类名完全匹配,并且属性必须与实体类中的属性定义完全匹配。

The partial class name must match the class name of the entity class exactly, and the attribute must match the definition of the attribute in the entity class exactly.

重建后,这就像将属性放入实体类中一样,但是额外的好处是不会每次都被覆盖。

After a rebuild, this will now work as if you put the attribute within the entity class, but with the added bonus of not being overwritten every time.

这篇关于如何在实体框架中使用AllowHtml属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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