1

WebApi2、MVC5、および DAL プロジェクト (すべて RTM) を含むソリューションがあります。

現在組み込まれている新しいメンバーシップ ビットを使用したいと考えていますが、すべてのアカウントがアカウント コントローラーにあるのは好きではありません。ファイルの新しいプロジェクト( 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; }
}

ログインアクション結果内のアカウントコントローラーから試します

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 を使用できるようにするには、どうすればよいですか?

4

4 に答える 4

0

あなたUserManagerdbcontext

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;
    }
于 2013-10-22T20:16:34.350 に答える
0

VisualStudioのテンプレートでWebApiのプロジェクトなどを作成すると、

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));Startup.Auth.cs ファイルをよく見てください。

一部を見逃している可能性があり(new ApplicationDbContext())ます。デフォルトでは、空のパラメーターがあります。

于 2014-06-03T04:47:14.853 に答える
-1

これが役立つと思います。私は MVC5 を初めて使用し、モデル レイヤーを MVC Web サイトから分離したいと考えていました。これは主に、さまざまなプロジェクトでモデルを共有したいと考えているためです。さまざまなヘルプ サイトで見つけたプログラミング マンボ ジャンボのすべてをたどることができませんでした。限られた知識では解決できない多くのエラーが常に発生します。しかし、ApplicationDbContext を MVC5 モデルから移動する簡単な方法を見つけました。エラーはほとんどありません。すべての作業は、Microsoft によって既に提供されているウィザードによって行われます。私の小さな発見をみんなと共有したいと思います。これはあなたがすることです(ステップバイステップ):

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 を含む) ができました。エラーはありません!! 幸運を!

于 2015-08-12T12:55:08.460 に答える