문제

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