如何在实体框架中添加新的实体属性而不更改数据库模型

How to add new entity properties in Entity Framework without changing database model(如何在实体框架中添加新的实体属性而不更改数据库模型)
本文介绍了如何在实体框架中添加新的实体属性而不更改数据库模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是实体框架的新手.我从数据库优先方法开始,它创建了与我选择的表相对应的类.我正在使用 MVC.我的一张表中有一个 Date 列.我的要求是我想在我的 gridview(Grid MVC) 中显示日期,即如果获取的特定记录的日期是 2015 年 10 月 27 日,那么日应该显示星期二.我想这样做而不在我的数据库中添加当天的额外列.有没有办法解决这个问题.任何帮助将不胜感激.

我生成的模型类如下:-

公共部分类 QnsNew1{公共 int ID { 获取;放;}public Nullable<System.DateTime>日期{得到;放;}public Nullable<int>PersonelID { 得到;放;}公共字符串 CType { 获取;放;}公共字符串评论 { 获取;放;}//公共字符串 Day { get;放;}//我想避免这样做公共虚拟人事人员{get;放;}公共虚拟类型类型 { 获取;放;}}

解决方案

更好的方法

虽然这可以通过 Entity Framework 轻松完成,但正如其他答案所指出的,我 100% 承认,最好通过创建与常规实体分开的 ViewModel 来将表示/UI 模型与数据库模型分开,并且把你计算出来的属性放在那里.

如果你被EF卡住了,请继续阅读

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

这是一个完整的示例(在从此处的答案中借用星期几实施之后):

公共部分类 MyTable{[未映射]公共字符串 DayOfWeek{得到{如果(日期.HasValue)返回 DateTime.Now.DayOfWeek.ToString();别的返回空值;}}}

<块引用>

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

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; }
    }

解决方案

Abetterapproach

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.

IfyouarestuckwithEF,keepreading

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;
        } 
    }
}

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.

这篇关于如何在实体框架中添加新的实体属性而不更改数据库模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)