C# EF代码允许ICollection的第一个约定<;T>;属性是否为空集合而不是null?

C# EF代码允许ICollection的第一个约定<;T>;属性是否为空集合而不是null?,c#,entity-framework,entity-framework-4.1,ef-code-first,code-first,C#,Entity Framework,Entity Framework 4.1,Ef Code First,Code First,我注意到,默认情况下,实体框架代码首先忽略实例化ICollection属性,除非集合中至少有一个项。我更愿意保证集合始终是空的HashSet(即,具有零项的HashSet),而不是不存在项的null EF Code First是否有任何约定或设置可以启用此功能?在刚设置的实体的构造函数中实例化集合: public sealed partial class EntityClass { [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCall

我注意到,默认情况下,实体框架代码首先忽略实例化ICollection属性,除非集合中至少有一个项。我更愿意保证集合始终是空的
HashSet
(即,具有零项的
HashSet
),而不是不存在项的
null


EF Code First是否有任何约定或设置可以启用此功能?

在刚设置的实体的构造函数中实例化集合:

public sealed partial class EntityClass
{
    [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors",
        Justification = "EF 4.1 requires them to be virtual, and RIA Services requires the collections to be instantiated.")]
    public EntityClass()
    {
        OtherEntities = new List<OtherEntity>();
    }

    public virtual ICollection<OtherEntity> OtherEntities { get; set; }
}
公共密封部分类EntityClass
{
[SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”,
justionment=“EF 4.1要求它们是虚拟的,RIA服务要求集合被实例化。”)]
公共实体类()
{
OtherEntities=新列表();
}
公共虚拟ICollection其他实体{get;set;}
}
FXcop有一条抑制消息