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 8039195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:29:37+00:00 2026-06-05T03:29:37+00:00

I have a class that contains a few private/protected fields and some public getters

  • 0

I have a class that contains a few private/protected fields and some public getters that return the value of the fields. I am attempting to map the fields to database columns using the fluent API on the DbModelBinder in OnModelCreating. I cannot use an automatic property with a protected setter so please don’t offer that as a solution to my question. It would probably work I’m sure but I cannot use the automatic property as the domain class is shared code and used with other different ORMs that have different ways of mapping their fields and unfortunately one doesn’t support the au

I use the following code (found on stackoverflow) to access the protected field so that I can use the expression when mapping:

public static class ObjectAccessor<T>
where T : class
{
    public static Expression<Func<T, TResult>> CreateExpression<TResult>(string propertyOrFieldName)
    {
        ParameterExpression param = Expression.Parameter(typeof(T), "propertyOrFieldContainer");
        Expression body = Expression.PropertyOrField(param, propertyOrFieldName);
        LambdaExpression lambda = Expression.Lambda(typeof(Func<T, TResult>), body, param);

        return (Expression<Func<T, TResult>>)lambda;
    }
}

This all works wonderfully if the field name is m_username but if I use _username I get a ModelValidationException: One or more validation errors were detected during model generation: System.Data.Edm.EdmProperty: Name: The specified name is not allowed: ‘_username’.

I can’t use camelcase without the underscore either as the helper above can’t distinguish between the public Username property and the protected username field. I’d really like to still keep using the underscore camelcase without the letter prefix.

Is it possible to configure the ModelBinder in some way so that the validation accepts the property name with a leading underscore?

Thanks in advance,

Mark

  • 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-05T03:29:39+00:00Added an answer on June 5, 2026 at 3:29 am

    It seems that decoration of your field like this :

    [Column("username")]
    public string _username
    

    maybe helpful in your case, anyway – please review similar case

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

Sidebar

Related Questions

If I have a class that contains properties with private set and protected set
I have a class that contains data from some model. This class has metadata
I have a class that contains a boolean field like this one: public class
I have a Form class that contains many fields. I would like to render
I have a class that contains a few multi-dimensional arrays. I am trying to
I have an object that contains a few unserializable fields that I want to
I have a Javascript class that contains a few functions and member objects: function
If I have an object that contains a few fields that are Lists of
I have a class that contains decoded video frames. I would like my decoder
I have this class that contains vars for db connection; Imports Microsoft.VisualBasic Imports System.Data.SqlClient

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.