如何在不更改数据库模型的情况下在Entity Framework中添加新的实体属性 [英] How to add new entity properties in Entity Framework without changing database model

查看:150
本文介绍了如何在不更改数据库模型的情况下在Entity Framework中添加新的实体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是实体框架的新手.我从数据库优先方法开始,该方法创建了与所选表相对应的类.我正在使用MVC.我的一张表中有一个Date列.我的要求是我想在我的gridview(网格MVC)中显示日期,即如果获取的特定记录的日期为2015年10月27日,则日期应显示星期二.我想在数据库中不添加一天的额外列来执行此操作. 有办法吗任何帮助将不胜感激.

I am new to Entity Framework. I started with database first approach which created my classes corresponding to the tables I selected. I am using MVC. One of my tables has a Date column in it. My requirement is I want to display the Day in my gridview(Grid MVC) i.e. if Date for that particular record fetched is 10/27/2015, then Day should show Tues. I want to do this without adding an extra column for the day in my database. Is there a way for this. Any help will be greatly appreciated.

我生成的模型类如下:-

My model class generated is as below:-

public partial class QnsNew1
    {
        public int Id { get; set; }
        public Nullable<System.DateTime> Date { get; set; }
        public Nullable<int> PersonelID { get; set; }
        public string CType { get; set; }
        public string Comments { get; set; }
        //public string Day { get; set; }//I want to avoid doing this
        public virtual Personnel Personnel { get; set; }
        public virtual Type Type { get; set; }
    }

推荐答案

更好的方法

尽管可以使用Entity Framework轻松完成此操作,但是正如此处其他答案所指出的那样,并且我100%地承认,最好通过创建与常规实体分开的ViewModel来将表示/UI模型与数据库模型分开.将您计算出的属性放在那里.

A better approach

Although this can be easily done with Entity Framework, but as pointed out by other answers here, and I acknowledge 100%, that it is better to separate presentation/UI models from database models by creating ViewModel separate from your regular entities and put your calculated properties there.

在EntityFramework Database First模式下,为数据库模型生成的类是局部类.您基本上可以用相同的名称,相同的程序集和名称空间创建另一个分部类,并在其中添加您计算出的属性.

In EntityFramework Database First mode, the classes generated for the database model are partial classes. You can basically create another partial class with the same name and in the same assembly and namespace and add your calculated property there.

这是一个完整的示例(从此处的答案中借鉴了星期几的实现方式):

here is a complete example (after borrowing the day of week implementation from the answers here):

public partial class MyTable
{
    [NotMapped]
    public string DayOfWeek
    { 
        get 
        { 
            if (Date.HasValue)
                return DateTime.Now.DayOfWeek.ToString();
            else
                return null;
        } 
    }
}

确保名称空间与生成的类相同是很重要的.另外,您还必须使用NotMapped属性注释该属性,以便EF不会尝试将该属性映射或保存回数据库.

It is important that you make sure the namespace is the same as the generated class. Also you will must annotate that property with NotMapped attribute so that EF does not attempt to map or save that property back to the database.

这篇关于如何在不更改数据库模型的情况下在Entity Framework中添加新的实体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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