سؤال

I'm using EF6, and I'm wanting to move the initialisation of my collections to the Entity itself, so the code can be written once, the tutorials don't do this, just wondering if it's ok? I don't know enough about the Entity Frameworks under the hood stuff yet to know if this is ok.

For example:

        [NotMapped]
    private List<MySuggestion> _mySelections;

public virtual ICollection<MySuggestion> mySuggestions {
        get 
        {
            if (_mySelections == null)
            {
                _mySelections = new List<MySuggestion>();
            }

            return _mySelections;
        }
        set;
    }

With the alternative being that the list be init'd as required in the Controller/Repository, with something like:

if (thing.mySuggestions == null){
   thing.mySuggestions = new List<Suggestion> ();
}
هل كانت مفيدة؟

المحلول

That should work fine. You'll need to specify a body for the setter though...

set
{
    _mySelections = value;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top