자동 생성 된 Entity Framework 모델이 있습니다. 데이터베이스 첫 번째 접근 방식을 사용하여 생성되었습니다. mid_initial
열에는 열을 최대 3 자 길이로 제한하는 데이터베이스 정의 제약 조건이 있습니다. 나는 시도하고 mid_initial
큰 3 자 이하, 유효하지 않은 상태로 모델을 만들 때데이터베이스 첫 번째 유효성 검사
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Agency.DataAccess.RegistrationModel
{
using System;
using System.Collections.Generic;
public partial class Registrant
{
public Registrant()
{
}
public int id { get; set; }
public string fname { get; set; }
public string mid_initial { get; set; }
public string lname { get; set; }
}
}
, ModelState.IsValid
사실이 반환됩니다. 이 때문에 db.SaveChanges
이 호출되면 DbEntityValidationException
이 발생합니다.
[HttpPost]
public ActionResult Create(Registrant registrant)
{
try
{
if (ModelState.IsValid)
{
Debug.WriteLine("Entity was valid.");
db.Registrants.Add(registrant);
db.SaveChanges();
return RedirectToAction("Index");
}
return View("Create", registrant);
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
return View(registrant);
}
}
ModelState.IsValid
메서드가 true를 반환하는 이유는 무엇입니까? 내 모델이 최대 길이 제한을 인식하지 못하는 것 같습니다. 어떻게 알 수 있습니까?
닫기 ...하지만 이해할 수는 있어도 작동하지 않습니다. 컴파일러는 다음과 같이 말할 것입니다 : "에러 2 타입 '등록자'에 이미 'mid_initial'에 대한 정의가 포함되어 있습니다.). 클래스 등록자를 MetadataType 속성으로 표시하고 유효성 검사를 위해 다른 클래스를 생성해야합니다. com/questions/25722866/ef5-db-first-generated-models-and-custom-validation/25741213 # 25741213 –