实体类型ApplicationUser不是当前上下文Asp.Net MVC的模型的一部分 [英] The entity type ApplicationUser is not part of the model for the current context Asp.Net MVC

查看:849
本文介绍了实体类型ApplicationUser不是当前上下文Asp.Net MVC的模型的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的应用程序的必需代码。我的帐户控制器文件中出现上述错误。我正在使用内置的帐号注册& MVC 4中的登录代码。无法注册用户。我已经看到在stackoverflow下面的链接解决了我的错误,但无法解决问题

Below is Required Code For my application . I am getting error stated above in my Account Controller file . I am using built in Account Register & Login code in MVC 4 . Unable to register the User . I have seen link below on stackoverflow tosolve my error but unable to get it solved

Link I Followed

  <connectionStrings>
 <add name="PromoteMyNameEntities1" connectionString="metadata=res://*/PromoteDB.csdl|res://*/PromoteDB.ssdl|res://*/PromoteDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=Dell-PC\SQLEXPRESS;initial catalog=PromoteMyName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

 <add name="DefaultConnection" connectionString="data source=Dell-PC\SQLEXPRESS;initial catalog=PromoteMyName;integrated security=True;" providerName="System.Data.SqlClient" />

 <connectionStrings>

IdentityModel.cs文件

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace PromoteMyName.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        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 static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

我的DbContent文件

public partial class PromoteMyNameEntities1 : DbContext
    {
        public PromoteMyNameEntities1()
            : base("name=PromoteMyNameEntities1")
        {
        }

strong> AccountController.CS(在此文件中获取错误)

AccountController.CS ( Getting Error in This File )

 // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

  //Getting Error at Line Below
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                return RedirectToAction("BusinessCategory", "ClientBusinesses");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }


推荐答案

我已经在你的web.config中看到你有两个连接字符串,但都使用相同的连接和相同的数据库,所以你不需要第二个连接。从您的web.config中删除下面的一行

Point 1 : I have seen in your web.config you are having two connections string but both are using same connection and same db so you don't require second one . Remove the below line from your web.config

<add name="DefaultConnection" connectionString="data source=Dell-PC\SQLEXPRESS;initial catalog=PromoteMyName;integrated security=True;" providerName="System.Data.SqlClient" />

点2:我认为你已经做了一些内置的成员资格Db确定的简单解决方案为您的问题是去你的数据库删除Aspnet / Inbuilt表(我的意思是删除数据库中的表,由代码第一个成员创建)

Point 2 : I think you have done some trouble with inbuilt Membership Db Sure Short Solution for you problem is go to your db remove the Aspnet / Inbuilt tables ( i mean to drop the tables in db that are created by code first membership )

之后删除那些表。现在清理解决方案,然后重新打开解决方案,然后运行解决方案它将自动在数据库中创建新的memebership表。

After removing those tables . Now Clean the Solution and Then ReBuild the solution , Then Run the Solution It will automatically create the new memebership tables in the database .

这篇关于实体类型ApplicationUser不是当前上下文Asp.Net MVC的模型的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆