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