C# 如何从虚拟ICollection返回EnityCollection?

C# 如何从虚拟ICollection返回EnityCollection?,c#,entity-framework,code-first,C#,Entity Framework,Code First,我首先使用实体框架代码。 我有两个非常相似的类,它们具有虚拟ICollection属性。 下面是其中一个集合类: public class Name { public int Id{ get; set;} [MaxLength(64)] [Index(IsUnique = true)] [Required] public string Value { get; set; } public virtual ICollection<NameVariant

我首先使用实体框架代码。 我有两个非常相似的类,它们具有虚拟ICollection属性。 下面是其中一个集合类:

public class Name
{
   public int Id{ get; set;}

   [MaxLength(64)]
   [Index(IsUnique = true)]
   [Required]
   public string Value { get; set; }

   public virtual ICollection<NameVariant> Variants { get; set; }
 }

 public class NameVariant
 {
   public int Id{ get; set;}

   [MaxLength(64)]
   [Index(IsUnique = true)]
   [Required]
   public string Value { get; set; }

   public int ParentId { get; set; }

   public virtual Name Parent { get; set; }
 }
公共类名
{
公共int Id{get;set;}
[MaxLength(64)]
[索引(IsUnique=true)]
[必需]
公共字符串值{get;set;}
公共虚拟ICollection变量{get;set;}
}
公共类名称变量
{
公共int Id{get;set;}
[MaxLength(64)]
[索引(IsUnique=true)]
[必需]
公共字符串值{get;set;}
public int ParentId{get;set;}
公共虚拟名称父项{get;set;}
}
我在一种情况下从变体获取EntityCollection,在另一种情况下从HashSet获取。
这取决于什么?如何从这两个类中获取EnityCollection?

成员国声明它将返回一个接口。您需要对该接口进行编码,并假设只要该类型实现该接口,代码就可以返回它想要的任何类型,而不是依赖于返回的特定具体实现。

成员国声明它将返回一个接口。您需要为该接口编写代码,并假设只要该类型实现该接口,代码就可以返回它想要的任何类型,而不是依赖于返回的特定具体实现