我想向现有类(对象?)添加一个新类型,该类从 Microsoft MVC 中的数据库表(带有 Entity Framework 类)获取。 IE: 我的类(class) {f_name, l_name} 当我循环抛出它时,如何向该对象添加新类型? 喜欢:
// Person class stracture is:
public Person()
{
public string f_name { get; set; }
public string l_name { get; set; }
}
我想在其中添加“年龄”而不将其添加到模型中
var all_persons = db.Person.toList();
for (var item in all_person)
{
item.age = some_value;
}
return View(all_persons);
最佳答案
在 Person
类中添加一个 [NotMapped]
(假设您不希望将此属性映射到数据库):
[NotMapped]
public int Age {get; set;}
或者创建 View 模型而不是直接使用 EF 模型
关于c# - 如何在 asp.net mvc 中向现有类或对象添加新类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41919008/