2015-11-13 25 views
0

我使用MVC 5 ASP.net身份實體框架代碼優先創建一個在線申請表格。我已經使用ASP.net標識腳手架創建了一個新項目,並且需要能夠添加其他屬性。其中一些是簡單的屬性 - 手機號碼等,這工作正常。但是我需要向用戶添加更復雜的對象來存儲應用程序表單信息。我試着這樣做:添加對象作爲屬性在ASP.Net身份

public class ApplicationUser : IdentityUser 
{ 
    public string Title { get; set; } 
    public string FirstName { get; set; } 
    public string Surname { get; set; } 
    public override string PhoneNumber { get; set; } 
    public string PracticeName { get; set; } 
    public string Address { get; set; } 
    public string Mobile { get; set; } 
    public string GMCNumber { get; set; } 
    public AppForm ApplicationForm { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection", throwIfV1Schema: false) 
    { 
    } 

    public DbSet<AppForm> AppForms { get; set; } 
    public DbSet<AppFormDocument> AppFormDocuments { get; set; } 
    public DbSet<AppFormAnswer> AppFormAnswers { get; set; } 

    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 
} 

,並創建了appform模式是這樣的:

public class AppForm { 

    public int Id { get; set; } 
    public int PercentComplete { get; set; } 
    public string Status {get; set; } 
    public bool Completed { get; set; } 
    public bool Reviewed { get; set; } 
    public bool SignedOff { get; set; } 
    public int LastPageCompleted { get; set; } 

    public List<AppFormDocument> Documents { get; set; } 
    public List<AppFormAnswer> Answers { get; set; } 

} 

public class AppFormDocument { 
    public int Id { get; set; } 
    public DateTime DateSubmitted { get; set; } 
    public string Name { get; set; } 
    public DateTime? ExpiryDate { get; set; } 
    public bool Accepted { get; set; } 
    public string ScannedFile { get; set; } 
} 


public class AppFormAnswer { 
    public int Id { get; set; } 
    public string QuestionNumber { get; set; } 
    public string Question { get; set; } 
    public string Answer { get; set; } 
} 

申請表是非常大的,有很多問題,這就是爲什麼我不只是把它所有的applicationuser類。還需要使用表單上載文檔。

現在,當我創建用戶並將AppForm的實例和AppFormAnswers的某些實例附加到它時,然後保存它,數據就會成功存儲,但是當我在登錄後再次嘗試訪問數據時,它未找到。儘管用戶還有其他簡單的屬性,包括手機號碼和標題。這裏是我的代碼:

[Route("appform/{action}")] 
[Authorize] 
public class AppFormController:Controller { 
    // GET: AppForm 
    public ActionResult Index() { 
     ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); 

     var model = new AppFormIndexViewModel(); 
     if (user != null) { 
      if (user.ApplicationForm != null) { 
       model.PercentComplete = user.ApplicationForm.PercentComplete; 
       model.NextPage = "Page" + user.ApplicationForm.LastPageCompleted + 1; 
       model.Status = user.ApplicationForm.Status; 
      } else { 
       // if appform is not available for user for some reason, create a new one. 
       user.ApplicationForm = new AppForm { PercentComplete = 0, Reviewed = false, Status = "Incomplete", SignedOff = false, Completed = false, LastPageCompleted = 0 }; 
       var uManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
       uManager.UpdateAsync(user); 

       model.PercentComplete = 0; 
       model.Status = "Incomplete"; 
       model.NextPage = "Page1"; 
      } 
     } 
     return View(model); 
    } 

現在uManager.UpdateAsync(用戶)線運行時,數據被保存到數據庫罰款和添加新的appForm紀錄。數據庫也自動爲所有表創建主鍵和外鍵。

那麼,我需要編寫一個重載的登錄方法來從其他表中檢索數據嗎?或者我需要添加一些東西到ApplicationDbContext類?

這是我的第一個MVC應用程序,所以不太確定現在轉向哪裏。已經嘗試閱讀許多論壇帖子和博客,但沒有找到任何真正符合我需求的內容。許多信息涉及mvc的早期版本,它們不適用於較新的asp.net標識系統。

+0

如果將AppFormId屬性添加到AppUser,該怎麼辦? –

+0

除了導航屬性之外,請嘗試添加外鍵屬性。例如,AppFormDocument應該具有int AppFormId作爲屬性 – Nikki9696

+0

此外,無論您選擇什麼東西,都可能需要「包含」。例如:http://stackoverflow.com/questions/13047845/how-to-include-a-child-objects-child-object-in-entity-framework-5 – Nikki9696

回答

0

我終於弄明白了。我添加了一個appformid屬性到applicationuser類這樣的:

[ForeignKey("AppForm")] 
public int AppFormId { get; set; } 

然後創建另一個類加載appform對象是這樣的:

public static class AppFormManager { 

    public static ApplicationUser GetCurrentUser() { 
     ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); 
     if (user != null) { 
      AppForm form = new AppForm(); 
      ApplicationDbContext db = new ApplicationDbContext(); 
      AppForm ap = db.AppForms.Where(af => af.Id == user.AppFormId).Include("Answers").Include("Documents").SingleOrDefault(); 
      if (ap == null) { 
       var AppForm = new AppForm { PercentComplete = 0, Reviewed = false, Status = "Incomplete", SignedOff = false, Completed = false, LastPageCompleted = 0 }; 
       user.AppForm = AppForm; 
      } else { 
       user.AppForm = ap; 
      } 
      return user; 
     } 
     return null; 
    } 

    public static bool SaveCurrentUser() { 
     ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); 
     if (user == null) { return false; } 
     var uManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
     uManager.UpdateAsync(user); 
     return true; 
    } 

} 

所以在所述控制器,所述代碼是乾淨得多:

// GET: AppForm 
    public ActionResult Index() { 

     ApplicationUser user = AppFormManager.GetCurrentUser(); 

     var model = new AppFormIndexViewModel(); 
     if (user != null) { 
       model.PercentComplete = user.AppForm.PercentComplete; 
       model.NextPage = "Page" + user.AppForm.LastPageCompleted + 1; 
       model.Status = user.AppForm.Status; 
     } 
     return View(model); 
    } 

我可以調用AppFormManager.SaveCurrentUser()方法將數據保存在發佈操作中。

感謝所有那些提出建議並幫助我找出解決辦法的人。可能不是最好的辦法,但它現在適用於我。