【发布时间】:2015-01-28 13:08:02
【问题描述】:
我正在使用 MVC5 Code First 并且有几个看起来像的类;
public class Asset
{
public int Id { get; set; }
public string FileName { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public int AssetId { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
}
我想返回一个视图,其中列出了具有特定类别的所有资产。我希望有一些类似于
的东西 public ActionResult ListByCategory(string categoryName)
{
var model =
from r in _db.Assets
.Where(r => r.Categories.CategoryName == categoryName)
select r;
return View(model);
}
我知道我的种子方法中有一些资产存在类别。但是编译器说“System.Collection.Generic.ICollection 不包含 CategoryName 的定义并且找不到扩展方法,我是否缺少参考?”这是我的 .Where 行。
我不完全明白它想告诉我什么。我确实引用了我的模型,因为我可以在控制器的其他地方引用它们。我知道单个资产可能属于多个类别,因此我在类级别创建了 ICollection。
这是我的上下文类;
public class AssetsDb : DbContext
{
public AssetsDb() : base("DefaultConnection")
{
}
public DbSet<Asset> Assets { get; set; }
public DbSet<Category> Categories { get; set; }
}
有人可以帮助我了解如何获取基础数据吗?我正在尝试学习 EF / MVC,所以感谢任何帮助。 谢谢。
【问题讨论】:
标签: asp.net-mvc linq entity-framework