当前位置:  开发笔记 > 编程语言 > 正文

使用.SaveChanges()保存电子邮件时出错;

如何解决《使用.SaveChanges()保存电子邮件时出错;》经验,为你挑选了1个好方法。

当我将电子邮件留空并提交注册表单时,它会将表单输入正确保存到数据库中.但是,当我包含一封电子邮件(例如:testemail@gmail.com)时,它会在db.SaveChanges(); 叫做.

我收到的错误说:

EntityFramework.dll中出现"System.Data.Entity.Validation.DbEntityValidationException"类型的异常,但未在用户代码中处理

附加信息:一个或多个实体的验证失败.有关详细信息,请参阅"EntityValidationErrors"属性.

控制器:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Mkq.Models;

namespace Mkq.Controllers
{
    public class TeacherController : Controller
    {
        private MkqDbEntities db = new MkqDbEntities();

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register([Bind(Include="teacher_id,subscription_id,teacher_fname,teacher_lname,username,password,email,teacher_credNumber,teacher_credST")] Teacher teacher)
    {
        teacher.subscription_id = 1;
        teacher.teacher_credNumber = null;
        teacher.teacher_credST = null;
        if (ModelState.IsValid)
        {
            db.Teachers.Add(teacher);
            db.SaveChanges();     //Error occurs when submitted
            return View();
        }

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

模型:

namespace Mkq.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

public partial class Teacher
{
    public Teacher()
    {
        this.Quiz_info = new HashSet();
        this.Students = new HashSet();
    }

    [DisplayName("Teacher ID")]
    public int teacher_id { get; set; }
    [DisplayName("Subscription ID")]
    public int subscription_id { get; set; }
    [DisplayName("First Name")]
    public string teacher_fname { get; set; }
    [DisplayName("Last Name")]
    public string teacher_lname { get; set; }
    [DisplayName("Username")]
    public string username { get; set; }
    [DisplayName("Password")]
    public string password { get; set; }
    [RegularExpression("^[a-zA-Z0-9_\\.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$", ErrorMessage = "E-mail is not valid")]
    [DisplayName("Email")]
    public string email { get; set; }
    [DisplayName("Credential Number")]
    public string teacher_credNumber { get; set; }
    [DisplayName("Credential")]
    public string teacher_credST { get; set; }

    public virtual ICollection Quiz_info { get; set; }
    public virtual ICollection Students { get; set; }
    public virtual Subscription Subscription { get; set; }
}
}

视图:

@model Mkq.Models.Teacher
@{
    ViewBag.Title = "Teacher Registration";
}

@ViewBag.Title.

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken()

Create a new account.


@Html.ValidationSummary()
@Html.LabelFor(m => m.teacher_fname, new { @class = "col-md-2 control-label" })
@Html.TextBoxFor(m => m.teacher_fname, new { @class = "form-control" })
@Html.LabelFor(m => m.teacher_lname, new { @class = "col-md-2 control-label" })
@Html.TextBoxFor(m => m.teacher_lname, new { @class = "form-control" })
@Html.LabelFor(m => m.username, new { @class = "col-md-2 control-label" })
@Html.TextBoxFor(m => m.username, new { @class = "form-control" })
@Html.LabelFor(m => m.password, new { @class = "col-md-2 control-label" })
@Html.PasswordFor(m => m.password, new { @class = "form-control" })
@Html.LabelFor(m => m.email, new { @class = "col-md-2 control-label" })
@Html.TextBoxFor(m => m.email, new { @class = "form-control" }) @Html.ValidationMessageFor(m => m.email)
} @section Scripts { @Scripts.Render("~/bundles/jqueryval") }

Brian Mains.. 7

如果您提供的值大于字段,则会发生这种情况; 那是我经常得到的.但是,您可以通过捕获异常并检查其错误消息列表来获取所有错误:

catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
   foreach (var e in ex.EntityValidationErrors)
   {
      //check the ErrorMessage property
   }
}

在那里放一个断点,它会给你错误.



1> Brian Mains..:

如果您提供的值大于字段,则会发生这种情况; 那是我经常得到的.但是,您可以通过捕获异常并检查其错误消息列表来获取所有错误:

catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
   foreach (var e in ex.EntityValidationErrors)
   {
      //check the ErrorMessage property
   }
}

在那里放一个断点,它会给你错误.

推荐阅读
Gbom2402851125
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有