【发布时间】:2020-11-20 02:09:05
【问题描述】:
当视图使用非导航对象作为模型时,访问导航属性的子属性的规范(最简单)方法是什么?
我有这两个模型:
public class Transaction
{
// F I E L D S & P R O P E R T I E S
[Key]
public int TransactionID { get; set; }
public DateTime TransactionDate { get; set; }
public decimal TransactionAmount { get; set; }
public string TransactionCategory { get; set; }
public string TransactionToOrg { get; set; }
public string TransactionFromOrg { get; set; }
public string TransactionDescription { get; set; }
[ForeignKey("Account")]
public int AccountID { get; set; }
public Account account { get; set; }
}
}
和“父母”
{
// F I E L D S & P R O P E R T I E S
[Key]
public int AccountID { get; set; }
public string AccountName { get; set; }
public string AccountInstitution { get; set; }
public string AccountType { get; set; }
public int AccountNumber { get; set; }
public decimal AccountBalance { get; set; }
public ICollection<Transaction> Transactions { get; set; }
[ForeignKey("User")]
public int UserID { get; set; }
public User user { get; set; }
}
在视图中,我正在尝试通过 ''' @T.account.AccountName ''' 访问帐户名,并且它抛出了一个空引用。我需要朝着正确的方向迈出一步来解决这个问题;这让我疯狂。最终,我还需要能够通过 CRUD 更改每个交易关联的帐户。谢谢!
【问题讨论】:
-
视图和视图模式没有导航属性。 ASP.NET MVC 是一个 Web 框架,而不是 ORM 或数据访问库。导航属性用于 EF,一个 ORM。
View是什么意思? ASP.NET 视图?这根本不会影响 ORM。数据库视图?这需要在 DbContext 的配置中进行配置。不过你还没有发布过 -
您还没有发布任何视图或操作代码,或者使用 ORM 的代码。查询很可能没有使用
Include急切地加载相关实体 -
嗨@PanagiotisKanavos!您能否提及通过“包含()”查询的答案,以便我可以将其标记为已解决?我知道这可能是一个非常初级的问题,但我想保留它,以便其他人可以看到修复。
标签: c# asp.net asp.net-mvc entity-framework model-view-controller