Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i am confirming my mail address from my mail i am getting this error

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Source Error: 


Line 130:                {
Line 131:                    user.ConfirmedEmail = true;
Line 132:                    await UserManager.UpdateAsync(user);
Line 133:                    await SignInAsync(user, isPersistent: false);
Line 134:                    return RedirectToAction("Index", "Home", new { ConfirmedEmail = user.Email });

Source File: G:\myebpassproject\ebpassprojects\ebpassprojects\Controllers\AccountController.cs    Line: 132 


here is my controller of regstraion:-

// POST: /Account/Register
       [HttpPost]
       [AllowAnonymous]
       [ValidateAntiForgeryToken]
       public async Task<ActionResult> Register(RegisterViewModel model)
       {
           try
        {
           if (ModelState.IsValid)
           {
               var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, Gender = model.Gender, DOB = model.DOB, Profile = model.Profile, Mobile = model.Mobile, Address = model.Address, City = model.City, State = model.State };

               user.ConfirmedEmail = false;
               var result = await UserManager.CreateAsync(user, model.Password);
               using (ApplicationDbContext db = new ApplicationDbContext())

                   if (result.Succeeded)
                   {
                       await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                       System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(
                             new System.Net.Mail.MailAddress("my mail id", "Web Registration"),
                             new System.Net.Mail.MailAddress(user.Email));
                       m.Subject = "Email confirmation";
                       m.Body = string.Format("Dear {0}<BR/>Thank you for your registration, please click on the below link to complete your registration: <a href=\"{1}\" title=\"User Email Confirm\">{1}</a>", user.UserName, Url.Action("ConfirmEmail", "Account", new { Token = user.Id, Email = user.Email }, Request.Url.Scheme));
                       m.IsBodyHtml = true;
                       System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp-mail.outlook.com");
                       smtp.Credentials = new System.Net.NetworkCredential("my mail id", "my password");
                       smtp.EnableSsl = true;
                       smtp.Port = 587;
                       smtp.Send(m);
                       return RedirectToAction("Confirm", "Account", new { Email = user.Email });

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


                   }
               AddErrors(result);
           }

           // If we got this far, something failed, redisplay form
           return View(model);
       }
           catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
           {
               Exception raise = dbEx;
               foreach (var validationErrors in dbEx.EntityValidationErrors)
               {
                   foreach (var validationError in validationErrors.ValidationErrors)
                   {
                       string message = string.Format("{0}:{1}",
                           validationErrors.Entry.Entity.ToString(),
                           validationError.ErrorMessage);
                       // raise a new exception nesting
                       // the current instance as InnerException
                       raise = new InvalidOperationException(message, raise);
                   }
               }
               throw raise;
           }

       }


here is my registration table

CREATE TABLE [dbo].[AspNetUsers] (
    [Id]                   NVARCHAR (128)  NOT NULL,
    [Email]                NVARCHAR (256)  NULL,
    [EmailConfirmed]       BIT             NOT NULL,
    [PasswordHash]         NVARCHAR (MAX)  NULL,
    [SecurityStamp]        NVARCHAR (MAX)  NULL,
    [PhoneNumber]          NVARCHAR (MAX)  NULL,
    [PhoneNumberConfirmed] BIT             NOT NULL,
    [TwoFactorEnabled]     BIT             NOT NULL,
    [LockoutEndDateUtc]    DATETIME        NULL,
    [LockoutEnabled]       BIT             NOT NULL,
    [AccessFailedCount]    INT             NOT NULL,
    [UserName]             NVARCHAR (256)  NOT NULL,
    [FirstName]            NVARCHAR (MAX)  NULL,
    [MiddleName]           NVARCHAR (MAX)  DEFAULT ('') NOT NULL,
    [LastName]             NVARCHAR (MAX)  DEFAULT ('') NOT NULL,
    [Gender]               NVARCHAR (MAX)  DEFAULT ('') NOT NULL,
    [DOB]                  NVARCHAR (MAX)  DEFAULT ('') NOT NULL,
    [Profile]              NVARCHAR (MAX)  NULL,
    [Mobile]               DECIMAL (18, 2) DEFAULT ((0)) NOT NULL,
    [Address]              NVARCHAR (MAX)  DEFAULT ('') NOT NULL,
    [City]                 NVARCHAR (MAX)  DEFAULT ('') NOT NULL,
    [State]                NVARCHAR (MAX)  DEFAULT ('') NOT NULL,
    [ConfirmedEmail]       BIT             DEFAULT ((0)) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);


GO
CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex]
    ON [dbo].[AspNetUsers]([UserName] ASC);


What I have tried:

i tried through try catch option but i am not getting exectly where i am wrong
Posted
Updated 7-Mar-17 5:45am
v2
Comments
Richard Deeming 7-Mar-17 10:05am    
So when you debugged your code and looked at the EntityValidationErrors collection, what errors did you see?
Himanshu.A.Joshi 7-Mar-17 10:19am    
i dont know it is not showing me actual error.
Richard Deeming 7-Mar-17 10:22am    
Well, you need to find it. We can't tell you what the problem is without seeing the errors.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900