Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First Fluent API validation not working

There is a class - it's a common class nothing special:

public class Trader{

public Guid UserId {get;set;}
public int TraderId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string Skype { get; set; }
public string Photo { get; set; }
public string Email { get; set; }

public virtual User User { get; set; }
}

Mapping:

 public TraderMap()
        {
            this.ToTable("Trader", "General");
            this.HasKey(a => a.TraderId);
            this.HasRequired(a => a.User).WithMany().HasForeignKey(a => a.UserId);
            Property(a => a.UserId).HasColumnName("UserID").IsRequired();
            Property(a => a.TraderId).HasColumnName("TraderID").IsRequired();

            Property(a => a.FirstName).HasMaxLength(50).IsRequired();
            Property(a => a.LastName).HasMaxLength(50).IsRequired();
            Property(a => a.PhoneNumber).HasMaxLength(25).IsRequired();
            Property(a => a.Skype).HasMaxLength(50).IsOptional();
            Property(a => a.Photo).HasMaxLength(100).IsOptional();
            Property(a => a.Email).HasMaxLength(100).IsRequired();
        }

When I leave FirstName or other fields that have IsRequired() empty in the form (View) the validation do not kick in. It just runs into the error:

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

Unfortunately this error doesn't say too much. I was digging a little deeper but the only thing I was able to get was

Invalid column name discriminator.

I thought it would be some forgotten inheritance somewhere (for User class) but I haven't found anything suspicious.

The problem is that when I use attributes in the Trader class everything works as supposed.

   public class Trader{

    public Guid UserId {get;set;}
    public int TraderId { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public string PhoneNumber { get; set; }
    public string Skype { get; set; }
    public string Photo { get; set; }
    [Required]
    public string Email { get; set; }

    public virtual User User { get; set; }
    }

With attributes the validation works ok and @Html.ValidationMessageFor starts to display error messages and it doesn't allow to send NULL values.

Do you have any suggestion what would be the problem with my mapping?

UPDATE 1 In fact the attributes above are a possible solution to this problem.

like image 626
nubm Avatar asked Jan 17 '23 00:01

nubm


2 Answers

Validation only kicks in if you use 'data annotations' - while HasRequired does the mapping - data annotations attributes do both the mapping and the validation part.
i.e. for your views to validate I believe you'd have to place annotations/attributes on your properties.

That's often used to 'distinguish' in between just 'mapping' and both mapping and validating. i.e. if you want both use the attributes, if you want just mapping use the fluent configuration.

A related answer here also https://stackoverflow.com/a/9310435/417747 or https://stackoverflow.com/a/9789984/417747

EDIT: and this one is even closer to what you need, thanks to Justin
How to make Fluent API configuration work with MVC client side validation?

like image 70
NSGaga-mostly-inactive Avatar answered Jan 21 '23 17:01

NSGaga-mostly-inactive


I think you are confusing EF Model validation with MVC validation. MVC knows nothing about EF or vice versa. They are seperate technologies that work well together.

When you define validation on the fluent data model, you are only defining validation for Entity Framework. This clearly works as when you try to save changes, EF fails and complains that Validation Failed.

Again, this has nothing to do with MVC validation, and the two do not work together for the most part (one exception is that if you are using a POCO class and use Data annotations, some of the annotations work in both MVC and EF, but some do not. It's not good practice to use your data model directly in your view, so this is largely a moot point.)

like image 20
Erik Funkenbusch Avatar answered Jan 21 '23 17:01

Erik Funkenbusch