This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Test Gist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var test = new Student(); | |
//add it to context | |
context.Students.Add(student); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected virtual ValidationResult IsValid(Object value, ValidationContext validationContext) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public InstituteEntities() | |
{ | |
this.Configuration.ValidateOnSaveEnabled = true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EFCodeFirstValidation.Helpers | |
{ | |
using System.Linq; | |
using System.Text; | |
using System.Data.Entity.Validation; | |
using System.Collections.Generic; | |
class DbEntityValidationMessageParser | |
{ | |
internal static string GetErrorMessage(DbEntityValidationException validationException) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var context = new InstituteEntities()) | |
{ | |
var student = context.Students.Create(); | |
student.StudentFirstName = "Muhammad"; | |
student.StudentLastName = "Siddiqi"; | |
context.Students.Add(student); | |
try | |
{ | |
//save changes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EFCodeFirstValidation.Entities.Validation.AssociateMetadata | |
{ | |
class StudentAssociatedMetadata | |
{ | |
[MinLength(3)] | |
public virtual string StudentFirstName { get; set; } | |
[Required] | |
public virtual string StudentLastName { get; set; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EFCodeFirstValidation.Entities.Validation.AssociateMetadata | |
{ | |
using System.Text; | |
using System.ComponentModel.DataAnnotations; | |
class DepartmentAssociatedMetadata | |
{ | |
[RegularExpression("^Department of")] | |
public virtual string DepartmentName { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace EFCodeFirstValidation.Entities | |
{ | |
using System.ComponentModel.DataAnnotations; | |
using EFCodeFirstValidation.Entities.Validation.AssociateMetadata; | |
[MetadataType(typeof(DepartmentAssociatedMetadata))] | |
public partial class Department | |
{ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
base.OnModelCreating(modelBuilder); | |
modelBuilder.Entity<Department>() | |
.Property(d => d.DepartmentName) | |
.HasMaxLength(20); | |
modelBuilder.Configurations.Add(new StudentConfiguration()); | |
} |
OlderNewer