我是Entity Framework的新手。我从数据库第一种方法开始,创建了与我选择的表对应的类。我正在使用MVC
。我的一个表格中有一个Date
列。我的要求是我想在我的gridview
(网格MVC)中显示日,即如果获取的特定记录的日期是10/27/2015,则日应该显示周二。我想这样做,而不是在我的数据库中添加额外的列。
有没有办法解决这个问题。任何帮助将不胜感激。
我生成的模型类如下: -
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; }
}
答案 0 :(得分:17)
尽管可以使用Entity Framework轻松完成,但正如其他答案所指出的那样,并且我100%承认,通过创建与常规实体分开的ViewModel,最好将表示/ UI模型与数据库模型分开。把你的计算属性放在那里。
在EntityFramework Database First模式中,为数据库模型生成的类是分部类。您基本上可以创建另一个具有相同名称的部分类,并在相同的程序集和命名空间中,并在那里添加您的计算属性。
这是一个完整的例子(从这里的答案中借用一周的实施日期):
public partial class MyTable
{
[NotMapped]
public string DayOfWeek
{
get
{
if (Date.HasValue)
return DateTime.Now.DayOfWeek.ToString();
else
return null;
}
}
}
确保命名空间与生成的类相同非常重要。此外,您必须使用
NotMapped
属性注释该属性,以便EF不会尝试将该属性映射或保存回数据库。
答案 1 :(得分:2)
使用相同的名称创建一个部分类,并具有一个名为Day的getter属性。这不会向您的数据库添加列。
public partial class QnsNew1
{
public string Day
{
get
{
if (Date.HasValue)
return DateTime.Now.DayOfWeek.ToString();
else
return null;
}
}
}
答案 2 :(得分:0)
您可以在生成的数据/模型旁边添加passwords = ['mrjoebblock' , 'mrjoefblock' , 'mrjoegblock', 'mrjoeadmin' ]
if choice == '3':
password = raw_input('Welcome admin! I\'m going to need your password ')
if password == 'mrjoeadmin':
print('Welcome Mr. Joe!')
Choice11 = raw_input('What would you like to do? Press 1 for changing your admin password, 2 for viewing a class\'s comments, or 3 for changing a class\'s password')
if Choice11 == '1':
print('You have chosen to change your password! ')
Choice12 = raw_input('You will need to put in your current password to access this feature ')
if Choice12 == 'mrjoeadmin':
Choice13 = raw_input('What would you like to change your password to? ')
passwords.remove('mrjoeadmin')
passwords.append(Choice13)
print('Thank you! Password has been changed. Log in with new password next time')
break
if Choice11 == '2':
print('you have chosen to view a class\'s comments!')
Choice14 = raw_input('Which block would you like to view?')
if Choice14 == 'F block':
print('To view F block\'s comments, go to where you search for things in your computer, and search CommentsFblock.txt. Open it')
time.sleep(10)
break
if Choice14 == 'G block':
print('To view G block\'s comments, go to where you search for things in your computer, and search CommentsGblock.txt. Open it')
time.sleep(10)
break
if Choice14 == 'B block':
print('To view B block\'s comments, go to where you search for things in your computer, and search CommentsBblock.txt. Open it')
time.sleep(10)
break
else:
print('I\'m sorry, that\'s not a valid block name.')
time.sleep(2)
break
if Choice11 == '3':
print('you have chosen to change a block\'s password.')
Choice15 = raw_input('Which block\'s password would you like to change?')
if Choice15 == 'F block':
Choice16 = raw_input('You will need to put in your current password to change F block\'s password ')
if Choice16 == 'mrjoeadmin':
Choice17 = raw_input('What would you like to change F block\'s password to? ')
passwords.remove('mrjoefblock')
passwords.append(Choice17)
print('Thank you! Password has been changed. Remember to tell students the password has been changed')
time.sleep(3)
break
if Choice15 == 'G block':
Choice18 = raw_input('You will need to put in your current password to change G block\'s password ')
if Choice18 == 'mrjoeadmin':
Choice19 = raw_input('What would you like to change G block\'s password to? ')
passwords.remove('mrjoegblock')
passwords.append(Choice19)
print('Thank you! Password has been changed. Remember to tell students the password has been changed')
time.sleep(3)
break
if Choice15 == 'B block':
Choice20 = raw_input('You will need to put in your current password to change B block\'s password ')
if Choice20 == 'mrjoeadmin':
Choice21 = raw_input('What would you like to change B block\'s password to? ')
passwords.remove('mrjoebblock')
passwords.append(Choice21)
print('Thank you! Password has been changed. Remember to tell students the password has been changed')
time.sleep(3)
break
。但我可能会建议您将DataModel与演示模型分开,这是一个很好的理由。您的演示模型将包含仅用于演示的部分,并且是即时计算的,而您的数据模型应该严格代表您持久保存的数据。
答案 3 :(得分:0)
考虑使用视图模型而不是实体框架创建的模型。由于您遇到的问题,我不喜欢在我的视图中使用数据库模型。相反,我创建了一个视图模型,然后使用AutoMapper或类似的库将数据从数据库模型复制到视图模型。