在EF数据库自定义属性第一 [英] Custom properties in EF database first

查看:233
本文介绍了在EF数据库自定义属性第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我创建的数据库中使用数据库第一次的形式给出,
的EF模型,并添加自己的几个只读属性所产生的实体类EF,不能在数据库中。
我每次从新表更新我的模型添加数据
我松属性创建,所以我必须要重新创建。

I have created an EF model from database using database first aproach, and added myself several read only properties to entity class generated by EF which are not in database. Every time I update my model adding data from new tables I loose properties created, so I have to recreate them.

作为一个例子数据库我有产权isFemale,但在我的课我创建

As an example in database I have property isFemale but in my class I've created

public string Gender
{
    get
    {
           if(isFemale) return "female";
           else return "male";
     }
}



我的问题是有没有办法更新从模型数据库,剩下由我生成的属性?

My question is there a way to update the model from database, leaving properties generated by me?

感谢您!

推荐答案

上的另一部分类,而不是生成的类添加的属性。例如,如果你生成的类的类型的人,在同一个命名空间在同一个项目中定义的另一个部分类:

Add the properties on another Partial Class instead of the generated class. For example, if your generated class is of type Person, define another Partial class in the same project with the same namespace:

public partial class Person
{
    public string Gender
    {
        get
        {
            if(isFemale) return "female";
            else return "male";
         }
    }
}

这篇关于在EF数据库自定义属性第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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