简体   繁体   English

如何在不更改数据库模型的情况下在Entity Framework中添加新的实体属性

[英]How to add new entity properties in Entity Framework without changing database model

I am new to Entity Framework. 我是Entity Framework的新手。 I started with database first approach which created my classes corresponding to the tables I selected. 我从数据库第一种方法开始,创建了与我选择的表对应的类。 I am using MVC . 我正在使用MVC One of my tables has a Date column in it. 我的一个表中有一个Date列。 My requirement is I want to display the Day in my gridview (Grid MVC) ie if Date for that particular record fetched is 10/27/2015, then Day should show Tues. 我的要求是我想在gridview (Grid MVC)中显示Day,即如果获取的特定记录的日期是10/27/2015,则Day应该显示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; }
    }

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. 虽然这可以通过实体框架轻松完成,但正如其他答案所指出的那样,并且我100%承认,通过创建与常规实体分开的ViewModel并将计算结果放在一起,最好将表示/ UI模型与数据库模型分开那里的财产。

If you are stuck with EF, keep reading 如果您遇到EF,请继续阅读

In EntityFramework Database First mode, the classes generated for the database model are partial classes. 在EntityFramework Database First模式中,为数据库模型生成的类是分部类。 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. 此外,您必须使用NotMapped属性注释该属性,以便EF不会尝试将该属性映射或保存回数据库。

Create a partial class by the same name and have a getter property called Day. 使用相同的名称创建一个部分类,并具有一个名为Day的getter属性。 This will not add a column to your database. 这不会向您的数据库添加列。

public partial class QnsNew1
{
    public string Day
    {
        get
        {
            if (Date.HasValue)
                return DateTime.Now.DayOfWeek.ToString();
            else
                return null;
        }
    }
}

You could add a partial class QnsNew1 { public DayOfWeek Day { get; set; } } 你可以添加一个partial class QnsNew1 { public DayOfWeek Day { get; set; } } partial class QnsNew1 { public DayOfWeek Day { get; set; } } partial class QnsNew1 { public DayOfWeek Day { get; set; } } next to your generated data/model. partial class QnsNew1 { public DayOfWeek Day { get; set; } }旁边的生成的数据/模型。 But I would probably suggest that you separate your DataModel from your presentation model and this is a good reason why. 但我可能会建议您将DataModel与演示模型分开,这是一个很好的理由。 Your presentation model will have parts to it that are just used for presentation and are computed on the fly whereas your data model should just strictly represent your data that is persisted. 您的演示模型将包含仅用于演示的部分,并且是即时计算的,而您的数据模型应该严格代表您持久保存的数据。

Consider using a view model instead of model created by entity framework. 考虑使用视图模型而不是实体框架创建的模型。 I do not prefer using database models in my views because of the issue that you are facing. 由于您遇到的问题,我不喜欢在我的视图中使用数据库模型。 Instead I create a view model and then copy data from database model to view model using AutoMapper or some library like that. 相反,我创建了一个视图模型,然后使用AutoMapper或类似的库将数据从数据库模型复制到视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM