模型是否包含字段而不将其添加到数据库? [英] Have model contain field without adding it to the database?

查看:66
本文介绍了模型是否包含字段而不将其添加到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Asp.NET MVC4应用程序中,我想将字段添加到模型中而不将其添加到数据库中.

In my Asp.NET MVC4 application I want to add a field to my model without adding it to the database.

仅在模型的c#实例中存在,而不在数据库本身中的字段排序.

Sort of a field that only lives in c# instances of the model but not in the database itself.

是否有任何注释或其他方法可将字段从数据库本身中排除?

Is there any annotation or other way to exclude the field from the database itself?

我的意思是我可以在运行时读写数据库不了解的对象.

I mean an object I can read and write in runtime which the database has no knowledge of.

推荐答案

只需使用 [NotMapped] 装饰您的字段/属性.

Just decorate your field/property with [NotMapped].

例如:

public class MyModel
{
    public int Id { get; set; }
    public string Name { get; set; }

    [NotMapped]
    public string Type { get; set; }
}

请参见 http://msdn.microsoft.com/zh-我们/library/system.componentmodel.dataannotations.schema.notmappedattribute.aspx

这篇关于模型是否包含字段而不将其添加到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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