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

3
以下是我应用程序所需的代码。我在我的Account Controller文件中遇到了上述错误。我正在使用MVC 4中内置的帐户注册和登录代码。无法注册用户。我在stackoverflow上看到了下面的链接来解决我的错误,但无法解决它。 我遵循的链接 我Web.Config代码中的连接字符串
  <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);
    }

目前它们没有链接,我可以通过用户ID将它们链接起来。 - user5493185
我需要先将它们链接起来,然后才能在IdentityModel.cs中使用上述代码吗? - user5493185
不,不,对不起,我删除了这个问题。你告诉EF你有两个数据库,一个用于用户,另一个用于你的模型。你需要指向同一个物理数据库。 - Jose Luis
好的,没问题,我需要找到这种方法。 - user5493185
这可能会有所帮助:https://dev59.com/rWIj5IYBdhLWcg3wv3e2 - Jose Luis
显示剩余10条评论
2个回答

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:我认为您在内置会员数据库方面遇到了一些麻烦,解决问题的简短方法是进入您的数据库,删除Aspnet/内置表(我的意思是删除由代码优先成员身份创建的表)。

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


谢谢,我会尝试这个。 - user5493185

0

尝试使用IdentityUser而不是ApplicationUser,这将解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接