Cześć,
szukałem wszędzie odpowiedzi na mój problem i próbowałem wielu rzeczy. Może uda mi się znaleźć tutaj rozwiązanie.
Na swojej stronie próbuję zrobić możliwość rejestracji. Po wpisaniu danych, od razu pojawia się błąd:
System.Data.Entity.Validation.DbEntityValidationException: „Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.”
Model, na którym buduję te rejestracje wygląda tak:
namespace GamissimaEdu.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class Students
{
public int id { get; set; }
[Required]
[Column(TypeName = "varchar(64)")]
public string first_name { get; set; }
[Required]
[Column(TypeName = "varchar(64)")]
public string last_name { get; set; }
[Required]
[Column(TypeName = "varchar(255)")]
public string user_name { get; set; }
[Required]
[Column(TypeName = "varchar(255)")]
public string password { get; set; }
[Required]
[Column(TypeName = "varchar(MAX)")]
public string location { get; set; }
[Required]
[Column(TypeName = "varchar(80)")]
public string email { get; set; }
}
}
Kontroler:
using GamissimaEdu.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace GamissimaEdu.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
GamissimaDBEntities db = new GamissimaDBEntities();
var student = db.Students;
return View(student);
}
public ActionResult Login()
{
return View();
}
[HttpGet]
public ActionResult Register()
{
return View();
}
[HttpPost]
public ActionResult Register(Students obj)
{
GamissimaDBEntities db = new GamissimaDBEntities();
db.Students.Add(obj);
db.SaveChanges();
return View(obj);
}
}
}
Widok:
@model GamissimaEdu.Models.Students
@{
ViewBag.Title = "Register";
}
@using (Html.BeginForm())
{
<section class="form3 cid-swlWoYNGVc" id="form3-x">
<div class="jumbotron-fluid">
<div class="row justify-content-center">
<div class="col-lg-7 col-12">
<div class="image-wrapper">
<img class="w-100" src="~/Content/assets/images/mbr-1920x1280.jpg" alt="Mobirise">
</div>
</div>
<div class="col-lg-3 offset-lg-1 mbr-form" data-form-type="formoid">
<form action="https://mobirise.eu/" method="POST" class="mbr-form form-with-styler" data-form-title="Form Name">
@Html.AntiForgeryToken()
<input type="hidden" name="email" data-form-email="true" value="CLbahEFXek0pZScgmcDsv1v1QGdsB9u02/SDuWMPsVBUT1DIFumCax9G91bvanfH/WVXTBhtvORwKVeLBcrZRKpamC8mILIAF//mJ6FWoeQCo2IUZrXcpnmtIcBrXH+C">
<div class="row">
<div hidden="hidden" data-form-alert="" class="alert alert-success col-12">
Thanks for filling out
the form!
</div>
<div hidden="hidden" data-form-alert-danger="" class="alert alert-danger col-12">
Oops...! some
problem!
</div>
</div>
<div class="dragArea row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1 class="mbr-section-title mb-4 display-2">
<strong>Załóż konto</strong>
</h1>
</div>
<div class="col-lg-12 col-md-12 col-sm-12">
<p class="mbr-text mbr-fonts-style mb-4 display-7">Podaj swój e-mail oraz hasło.</p>
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="col-lg-12 col-md col-sm-12 form-group" data-for="email">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control", placeholder = "Email" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
<div class="col-lg-12 col-md col-sm-12 form-group" data-for="userName">
@Html.EditorFor(model => model.user_name, new { htmlAttributes = new { @class = "form-control", placeholder = "Nazwa użytkownika" } })
@Html.ValidationMessageFor(model => model.user_name, "", new { @class = "text-danger" })
</div>
<div data-for="password" class="col-lg-12 col-md col-sm-12 form-group">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control", placeholder = "Hasło" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div>
<!-- <div data-for="confirmPassword" class="col-lg-12 col-md col-sm-12 form-group">
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control", placeholder = "Powtórz hasło" } })
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div> -->
<div data-for="firstName" class="col-lg-12 col-md col-sm-12 form-group">
@Html.EditorFor(model => model.first_name, new { htmlAttributes = new { @class = "form-control", placeholder = "Imię" } })
@Html.ValidationMessageFor(model => model.first_name, "", new { @class = "text-danger" })
</div>
<div data-for="lastName" class="col-lg-12 col-md col-sm-12 form-group">
@Html.EditorFor(model => model.last_name, new { htmlAttributes = new { @class = "form-control", placeholder = "Nazwisko" } })
@Html.ValidationMessageFor(model => model.last_name, "", new { @class = "text-danger" })
</div>
<div data-for="City" class="col-lg-12 col-md col-sm-12 form-group">
@Html.EditorFor(model => model.location, new { htmlAttributes = new { @class = "form-control", placeholder = "Miejscowość" } })
@Html.ValidationMessageFor(model => model.location, "", new { @class = "text-danger" })
</div>
<div class="col-md-auto col-12 mbr-section-btn"><button type="submit" class="btn btn-black display-4">Załóż konto</button></div>
</div>
</form>
</div>
<div class="offset-lg-1"></div>
</div>
</div>
</section>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Kiedy uruchamiam aplikację, ładuje się normalnie. Potem gdy klikam "submit", VStudio zatrzymuje się na linijce "db.Savechanges()" mówiąc, że nie obsługuje pewnego wyjątku. Wyświetlam szczegóły i zwróciłem uwagę, że parametr "IsValid" jest false. Nie wiem w którym miejscu mogłem popełnić błąd, szukam go. Próbowałem linijkę z db.SaveChanges wziąć w try catch, nic konkretnego nie znalazłem. Tak wygląda błąd po "kontynuacji".
Błąd serwera w aplikacji '/'.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Opis: Podczas wykonywania bieżącego żądania sieci Web wystąpił nieobsługiwany wyjątek. Aby uzyskać dodatkowe informacje o błędzie i miejscu jego występowania w kodzie, przejrzyj ślad stosu.
Szczegóły wyjątku: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Błąd źródła:
Wiersz 35: GamissimaDBEntities db = new GamissimaDBEntities();
Wiersz 36: db.Students.Add(obj);
Wiersz 37: db.SaveChanges();
Wiersz 38:
Wiersz 39: return View(obj);
Plik źródłowy: D:\GamissimaEdu\GamissimaEdu\Controllers\HomeController.cs Wiersz: 37
Ślad stosu:
[DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.]
System.Data.Entity.Internal.InternalContext.SaveChanges() +169
System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +27
System.Data.Entity.DbContext.SaveChanges() +20
GamissimaEdu.Controllers.HomeController.Register(Students obj) in D:\GamissimaEdu\GamissimaEdu\Controllers\HomeController.cs:37
lambda_method(Closure , ControllerBase , Object[] ) +103
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.Async.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0() +58
System.Web.Mvc.Async.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2() +228
System.Web.Mvc.Async.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +35
System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9850009
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
Byłbym bardzo wdzięczny za pomoc.