实体类型ApplicationUser不是当前上下文Asp.Net MVC的模型的一部分

时间:2016-05-05 09:33:07

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-4

以下是我的申请所需的代码。我在我的帐户控制器文件中收到上述错误。我正在使用内置帐户注册表& MVC 4中的登录代码。无法注册用户。我在stackoverflow上看到了链接,解决了我的错误但无法解决问题

Link I Followed

我的Web中的连接字符串。配置代码

  <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")
        {
        }

AccountController.CS(在此文件中出现错误)

 // 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);
    }

2 个答案:

答案 0 :(得分:1)

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

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

第2点:我认为你在内置会员Db Sure Short解决方案方面遇到了一些麻烦问题就是去你的db删除Aspnet / Inbuilt表(我的意思是删除db中的表这是由代码第一个成员资格创建的)

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

答案 1 :(得分:0)

尝试使用IdentityUser代替ApplicationUser,将解决问题