【发布时间】:2013-12-18 04:32:35
【问题描述】:
我已经搜索了将近2天,但找不到答案,所以让我在这里问。
我刚开始使用 ASP.NET MVC 5。我设法扩展了默认的 ApplicationUser : IdentityUser 类,所以现在它存储了有关用户的附加信息。扩展功能之一是存储用户所属的组织。一个组织可以有多个用户。在应用程序中,我希望能够将用户添加到任何组织。我也希望能够直接管理组织。这是我的模型:
public class ApplicationUser : IdentityUser
{
[Required]
[MaxLength(30)]
public string FirstName { get; set; }
[Required]
[MaxLength(30)]
public string LastName { get; set; }
[Required]
[MaxLength(40)]
public string Email { get; set; }
[Required]
public virtual Organisation Organisation { get; set; }
}
public class Organisation
{
public int id { get; set; }
[Required]
[MaxLength(30)]
public string Name { get; set; }
[Required]
public DateTime RegistrationDate { get; set; }
[Required]
[MaxLength(30)]
public string DWId { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
//...
}
迁移后,EF 为组织创建了一个单独的表。到目前为止,一切顺利。
现在,我希望能够从“组织”表中查看和添加/删除/删除组织。为此,我创建了一个控制器。但是,在控制器内部,我无法引用 Organizations 表,因此我可以将其作为模型传递给视图。这是我的想法,但未能做到:
public ActionResult Organisation()
{
ApplicationDbContext _db = new ApplicationDbContext();
var model = _db.Organisations.ToList(); //this does not work
return View(model);
}
当我尝试构建它时,它说
'TrendfinderMVC.Models.ApplicationDbContext' 不包含 “组织”的定义,没有扩展方法“组织” 接受类型的第一个参数 'TrendfinderMVC.Models.ApplicationDbContext' 可以找到(你是 缺少 using 指令或程序集引用?)
事实上,当我开始输入“_db.”时,IntelliSense 甚至没有在列表中显示任何与组织相关的内容。我如何获得当前所有组织的列表?任何见解将不胜感激。
【问题讨论】:
-
var model = _db.Organisations.ToList(); //this does not work会发生什么? -
@damienc88,我刚刚编辑了原始消息(结尾部分)以包含我尝试构建它时发生的事情。
-
您应该对您的代码进行拼写检查:
organization -
Dan-o,某些国家/地区接受组织作为正确的拼写。 en.wiktionary.org/wiki/organisation
标签: c# entity-framework asp.net-mvc-5 asp.net-identity