簡體   English   中英

如何編輯在IdentityDbContext中創建的表

[英]How to edit the table created in IdentityDbContext

我已經搜索了近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為組織創建了一個單獨的表。 到現在為止還挺好。

現在,我希望能夠從組織表中查看和添加/刪除/刪除組織。 為此,我創建了一個控制器。 但是,在控制器內部,我無法引用“組織”表,因此我可以將其作為模型傳遞給視圖。 這是我的想法,但沒有做到:

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甚至沒有顯示與列表中的組織相關的任何內容。 我如何獲得目前所有組織的列表? 任何見解將不勝感激。

確保已在ApplicationDbContext類中聲明了您的組織集

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext()
        : base("DefaultConnection"){}

    public DbSet<Organisation> Organisations { get; set; }
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM