2

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
  • No, I want to map the protected property - that is address. Commented Sep 28, 2011 at 18:04

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Yep, strings... Must've blanked out for a few minutes :) Changed it to objects with Id, and it worked. Btw, you don't need setter for collection properties, EF uses Add() to populate it.
Collection of strings cannot be mapped and persisted. EF doesn't know how to deal with that.
Yeah, I know, I was saying collection properties don't need to have a setter.
I modified the answer. You are right that in code first setter is not needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.