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; }
}
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.
If you are stuck with EF, keep reading
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.
这篇关于如何在实体框架中添加新的实体属性而不更改数据库模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在实体框架中添加新的实体属性而不更改数据库模型
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30