【问题标题】:How can I return EnityCollection from virtual ICollection?如何从虚拟 ICollection 返回 EnityCollection?
【发布时间】:2014-10-29 15:14:20
【问题描述】:

我首先使用实体​​框架代码。 我有 2 个具有虚拟 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; }
 }

在一种情况下,我从 Variants 获得 EntityCollection,在另一种情况下获得 HashSet。 它取决于什么?如何从两个类中获取 EnityCollection?

【问题讨论】:

    标签: c# entity-framework code-first


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多