1

我有一個包含WebApi2,MVC5 & DAL項目(全部爲RTM)的解決方案。將mvc5帳戶控制器代碼移到單獨的項目中

我想要使用新的成員資格位,但現在我不喜歡帳戶控制器中的所有帳戶資料。做一個文件新項目(asp.net)擁有所有的會員資格耦合到帳戶控制器。

在我的DAL中,我使用EF6,因爲我喜歡代碼優先的理想,因爲它適合我所要做的。我正在嘗試將帳戶控制器代碼移到我單獨的項目中。

的DAL在我的環境是不錯的,簡單的(從MVC網站所)

public class ApplicationUser : IdentityUser 
{ 
    //a user can belong to multiple stores 
    public virtual ICollection<StoreModel> Stores { get; set; } 
} 


public class DataContext : IdentityDbContext<ApplicationUser> 
{ 
    public DataContext(): base("name=DefaultConnection") 
    { 
    } 
    public DbSet<Business> Businesses { get; set; } 
    public DbSet<ConsumerModel> Consumers { get; set; } 
    public DbSet<StoreModel> Stores { get; set; } 
} 

從我的賬戶控制器我的登錄內的ActionResult我嘗試

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) 
    { 
     var user = await UserManager.FindAsync(model.UserName, model.Password); 
     if (user != null) 
     { 

它拋出一個錯誤與User.FindAsync

實體類型ApplicationUser不是模型的一部分當前上下文。

我需要做什麼才能允許ApplicationUser在當前上下文中使用?

+0

如何創建UserManager實例? – aleyush

+0

你知道了嗎?我有完全相同的問題。我也正是@ casey-sebben在下面建議的。 – RichC

回答

0

你需要創建一個UserManager這需要在其中發生在你的類似dbcontext

public UserController() 
     : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) 
    { 
    } 


    public UserManager<ApplicationUser> UserManager { get; private set; } 

    public UserController(UserManager<ApplicationUser> userManager) 
    { 
     UserManager = userManager; 
    } 
1

我做的東西userstore。爲了實現問題的分離,我從我的Repository中獲取UserManager,然後在Presentation層中使用它。存儲庫內部使用內部LoginDbContext從UserStore創建UserManager。這樣,DbContext和Store就從控制器中分離出來了。

+0

我有這個相同的問題。你能舉一個你在這裏建議的代碼示例嗎? – RichC

+0

嗨富,真的很抱歉,因爲這麼晚回覆。我被趕上了我的衝刺。無論如何,請看看下面的帖子。如果您還有其他問題,請告訴我。謝謝,-Sajid ...發佈:http://sajidq.wordpress.com/2014/02/14/converting-web-application-from-membership-to-asp-net-identity/ – SajidQ

0

如果您創建的WebAPI項目或與VisualStudio的模板財產以後,

在Startup.Auth.cs文件請謹慎參閱UserManagerFactory =() => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

您可能會錯過(new ApplicationDbContext())的一部分。默認情況下,它有空參數。

-2

我相信這會有所幫助。我對MVC5相當陌生,我一直想將我的模型層與MVC網站分開,主要是因爲我想我會在我的各個項目中共享模型。我一直無法遵循我在各種幫助網站上找到的所有編程節目。我總是會遇到很多我無法用有限的知識解決的錯誤。但是,我發現了一個簡單的方法,將我的ApplicationDbContext移出MVC5模型,並且幾乎沒有任何錯誤。所有的工作都是由微軟提供的嚮導完成的。我想與大家分享我的小發現。這就是你要做的(一步一步):

1. Create a MVC5 project with authentication. Call it ModelProject. 
2. Exclude everything from the project except 
    a. Properties 
    b. References 
    c. Models 
    d. packages.config 

3. The ModelProject will hold all your models (even ApplicationDbContext.) Rebuild it. 
4. Now, create a new MVC5 project with authentication. Call this Mvc5Project 
5. Import the ModelProject project into Mvc5Project . 
6. Wire the ModelProject into this project i.e. link in the reference. 
7. Exclude the following from the MVc5Project from the Models folder 
    a. AccountViewModels.cs 
    b. IdentityModels.cs 
    c. ManageViewModels.cs 
8. If you rebuild now, you will get a bunch of errors. Just go to the errors and resolve them using the right click method to get the new namespace from ModelProject. The namespace will show if you have wired the project in correctly. 
9. Also, dont forget to go to View/Manage and Views/Account of Mvc5Project and change the models in there to the new location otherwise you will get some weird cryptic errors. 

就是這樣!現在你有一個模型全部分離出來的項目(包括applicationDbContext) - 並且沒有錯誤!祝你好運!

+0

我帶回ApplicationDbContext返回進入主應用程序。雖然它正在工作,但Migrations停止工作,因爲在主項目中找不到ApplicationDbContext。 – alikuli

+0

它工作的很好! – alikuli

相關問題