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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:10:02+00:00 2026-05-25T20:10:02+00:00

I want to have a protected list in a class, accessible via methods. In

  • 0

I want to have a protected list in a class, accessible via methods.
In this topic it’s suggested that I could create a configuration class inside my model class, which I don’t really like, but as long as it works.. So I have something along those lines:

public class Person
    {
        public int Id { set; get; }
        public string Name { set; get; }

        List<string> _address = new List<string>();

        protected virtual ICollection<string> address { get { return this._address; } }

        [NotMapped]
        public string[] Address { get { return this.address.ToArray(); } }  // I know it's not efficient.. I'll cache it.

        public void AddAddress(string address)
        {
            // some business logic
            this.address.Add(address);
        }

        public class OneMapping : EntityTypeConfiguration<Person>
        {
            public OneMapping()
            {
                this.HasMany(x => x.address);
            }
        }
    }

and in db context:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Configurations.Add<Person>(new Person.OneMapping());
   base.OnModelCreating(modelBuilder);
}

The db context throws this exception when I’m trying to add an instance of person:

The navigation property ‘address’ is not a declared property on type
‘Person’. Verify that it has not been explicitly excluded from the
model and that it is a valid navigation property.

So doesn’t really work.
Tried this sample: Mapping private properties with EF 4.1
Failed with same error.

Another solution would be to use ObservableCollection, and wire the business logic in events, but this logic doesn’t need to run when objects are constructed by EF – only when they’re constructed by user.. which isn’t possible.

So I’m somewhat stuck here.. I hope someone out there already ran into this and solved it..

Thanks !

  • 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-05-25T20:10:02+00:00Added an answer on May 25, 2026 at 8:10 pm

    Your navigation property doesn’t have setter so it cannot be mapped. All mapped properties must have accessible getter and setter because EF can assign them during entity materialization. That was true for EDMX where at least private setter was necessary but in case of code first it works even without setter.

    Generally EF code first is not very good tool if you want to play with accessibility of mapped properties.

    Edit:

    Btw. Your address is not collection of entities but collection of strings – that is also not supported

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

Sidebar

Related Questions

I have code that looks like this: public class Polynomial { List<Term> term =
I want to have a select-only ComboBox that provides a list of items for
Let's say I have a class A and this class has a List l
Hi I have a class A with as abstract method protected abstract List<Contributor> GetContributors(List<SyndicationPerson>
I want to give access to protected properties to another class. I have been
I have one class that shows me a list of Items. Right now I
I have a class hierarchy, this one: type TMatrix = class protected //... public
If I have a Class A that contain a List of children with class
I have this class: public class CompositeSecurityAuthorizer implements SecurityAuthorizer { @inject @CompositeSecurityAuthorizerAnnot List<SecurityAuthorizer> authorizers;
In a simplified situation like this one, with 3 classes: class ClassONE{ protected List<IntermediatedClass>;

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.