Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:06:31+00:00 2026-06-01T15:06:31+00:00

There is a class – it’s a common class nothing special: public class Trader{

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:06:32+00:00Added an answer on June 1, 2026 at 3:06 pm

    You were both right, I was under false assumption.

    [Required] is not an equivalent of .IsRequired()

    There are several possible solutions:

    1) The quick & easy: Attributes

    Model Configuration overrides and Validation

    With CodeFirst it is possible to override the configuration of the
    model defined with validation attributes, e.g. in the OnModelCreating
    method. Reconfiguring model affects validation since validation should
    use the actual model configuration – blindly use of attributes would
    cause validation errors for values that could be valid according to
    the overrides made in OnModelCreating(). Here are the three special
    cases for overrides made in OnModelCreating:

    • If a property was decorated with [Required] attribute and was reconfigured as optional (.IsOptional() method) the [Required]
      attribute will be removed and as a result ignored when validation
      happens

    • If a property was decorated with [StringLength] or [MaxLength] attribute and then was configured with new length
      (.HasMaxLength() method) new maximum length will be used if possible

    • If a property was decorated with [StringLength] or [MaxLength] attribute and then was defined to be allowed maximum
      length (.IsMaxLength) then the attribute will be removed (if possible)
      and the length of the property value will not be checked

    Note that the above changes will be effective only if a property was
    decorated with some validation attributes. So, setting property as
    required (.IsRequired()) will not cause the property be validated
    against null value.
    See EF Feature CTP5: Validation

    2) A quick fix but it’s a dirty one as Mystere Man suggested
    http://thedatafarm.com/blog/data-access/capturing-code-first-fluent-api-validationresults-to-display-in-mvc3-views/

    http://bradwilson.typepad.com/blog/2010/10/service-location-pt6-model-validation.html

    3) The heavy one: The Validation Application Block in Enterprise Library
    http://bradwilson.typepad.com/blog/2009/10/enterprise-library-validation-example-for-aspnet-mvc-2.html but Enterprise Library is very often considered as an overkill

    4) Validation nirvana: FluentValidation looks very promising and I’ll definitely give it a try.
    http://www.nuget.org/packages/FluentValidation.MVC3
    Cons: Not the best approach in an n-layer app. It’s primarily focused on Views.

    5) N-Layer
    http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs
    This approach leads to problems with a validation on the client side so it must be solved separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a class or set of functions built into the .NET Framework (3.5+)
Is there any class or API available in C# to get available network drives
There is class Road with its Road.h and Road.cpp which contains implementation of method
Is there any class in the .NET framework that can read/write standard .ini files:
Is there a class in the standard library of .NET that gives me the
Is there a class/example application for a message-only window that is in C++ Win32
Is there a class that overwrites the .MainMenu_MenuItem class for the Main Menu? I
Is there a class in the ASP.NET MVC framework that represents a controller name
Is there a __CLASS__ macro in C++ which gives the class name similar to
Is there a class in the .NET BCL that can store a block of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.