3

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

My Connection Strings in My Web.Config Code

  <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 File

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

My DbContent File

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

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);
    }
Community
  • 1
  • 1
  • You have two contexts: PromoteMyNameEntities1 and ApplicationDbContext. The PromoteMyNameEntities1 don't extends IdentityDbContext, because of this you have this error. – Jose Luis May 05 '16 at 09:38
  • how can i extend that –  May 05 '16 at 09:39
  • Something like this: public ApplicationDbContext() : base("PromoteMyNameEntities1", throwIfV1Schema: false) – Jose Luis May 05 '16 at 09:42
  • currently they are not linked i can link them by userid –  May 05 '16 at 09:43
  • do i need to first link them then i need to use code above in idenitymodel.cs –  May 05 '16 at 09:44
  • no, no, I deleted this question, I'm sorry. You are telling EF that you have two databases, one for users, other for you model. You need to point to the same fisical database. – Jose Luis May 05 '16 at 09:45
  • I am having only one database . EIther I use one connection string or two same error exist –  May 05 '16 at 09:46
  • you can see in both connection string db name and connection is same –  May 05 '16 at 09:46
  • In the migrations folder, do you have two Configuration.cs classes, one for user's context and other for you model context? I have two. If you want to have two separated contexts. – Jose Luis May 05 '16 at 09:52
  • i am using dbfirst approach so i am not having migrations folder –  May 05 '16 at 09:54
  • Oh, I can't help then, I'm sorry. :-( – Jose Luis May 05 '16 at 09:55
  • don't you deal with db first –  May 05 '16 at 09:56
  • No, I use Code First, with migrations. I have two configuration classes. And separate migrations for users or model. – Jose Luis May 05 '16 at 09:58
  • ok no worries i need to find this way –  May 05 '16 at 09:59
  • This could help: http://stackoverflow.com/questions/19940014/asp-net-identity-with-ef-database-first-mvc5 – Jose Luis May 05 '16 at 10:10

2 Answers2

1

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" />

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 )

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 .

Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53
0

Try using IdentityUser instead of ApplicationUser,will Solve the problem

Omid
  • 81
  • 1
  • 5